Change the format of cells using formulas - excel

Hello I'd like to change the format of the cells from my column "I" to "0000" format, using a formula (consequently not using the .NumberFormat method) and VBA.
For instance if range("A1").Value = 09, the formula will transform it to 0009, etc.
I have tried this but it doesn't work :
With Range("I2", Cells(Rows.Count, "I").End(xlUp))
.Value = Evaluate("INDEX(TEXT(" & .Address(external:=True) & ",""0000""),)")
End With
Thank you in advance for your help,
Sincerely,
J.Garry

NON VBA
=right("000"&A1,4)
Basically tag on a bunch of 0s on the front which converts the number to a string, then take the last 4 characters. This is assuming you are working with integers and thus will not be thrown off by decimal points.

Related

VBA code changes from text into numeric value of a range, however no tall the columns?

i wrote a code in VBA to change values in a range from text into general (number). it works fine, however there is one issue. when i checked all the column values, only the string with an integer value ("2345") is changed to integer (2345) but the strings with float values ("123.456") didn't change at all, the type changed into General but the value stay the same.
here is the vba code
If (ws.Name = "TB") Then
shTB.Activate
[A:A].Select
With Selection
.NumberFormat = "General"
.Value = .Value
End With
results as the table below, still the float values didn't change at all. as you can see only row 119 changed into Number or integer.Any suggestion or help is appreciated highly!
It is clear from your results that there is a mis-match between the text format of your data and your Locale settings.
You data appears to have come from a source in which the comma is the decimal separator rather than the period. Try:
Sub dural()
With Range("A:A")
.NumberFormat = "General"
.Replace ",", "."
.Value = .Value
End With
End Sub
if that "." stands for thousands separator then just remove it
With shTB.Range("A:A")
.Replace what:=".", replacement:=vbNullString
.NumberFormat = "General"
End With
BTW, as you see you don't need to select anything: just act on its direct reference

Custom formatting date when writing to sheet

I'm having a problem getting a date formatted correctly when copying to a worksheet on a blank/un-formatted line.
I am using a macro to copy values from one sheet to another and I want one column to contain the current date that the script was run on.
Here is what I have:
records_list.Worksheets("Sheet1").Range("J" & records_row_number) =
Format(Date, "ddmmmyy")
This is working correctly in that it is putting a date value in to the target sheet (records_list). However, the formatting is coming in as "d-mmm-yy" which means it contains the dashes and drops the leading zero in the days.
I want the date in the format 09Oct18 but it is displaying as 9-Oct-18
I cannot for the life of me figure out why it's doing this or how to prevent it. I've tried lots of suggestions for date formatting I've seen on other sites but nothing is working. If I go into the cell formatting and force it to "ddmmmyy" it displays correctly, however, I want to avoid pre-formatting thousands of rows in the sheet so that it opens/saves faster.
Excel uses the range's numberformat when choosing how to display a date, not the format it was written to the range in. To change the numberformat of the range, you can do something like:
With records_list.Worksheets("Sheet1").Range("J" & records_row_number)
.Value = Date
.NumberFormat = "ddmmmyy"
End With
This needs to be done in two step. First enter Value and then set Number Format:
records_list.Worksheets("Sheet1").Range("J" & records_row_number).Value = Date
records_list.Worksheets("Sheet1").Range("J" & records_row_number).NumberFormat = "ddmmmyy"
or even better using With:
With records_list.Worksheets("Sheet1").Range("J" & records_row_number)
.Value = Date
.NumberFormat = "ddmmmyy"
End With

Excel VBA inserting date into cell produces incorrect format

Hoping someone can help with a funny issue I'm having. Using the following Excel VBA code to update a cell with a date in a sheet. The cell is formatted to "DD/MM"YYYY" my local PC is set to this also.
Dim SelectedDate As String
SelectedDate = "05/02/2018"
Sheets("CONTROL").Range("F36").NumberFormat = "dd/mm/yyyy"
Sheets("CONTROL").Range("F36").Value = Format(SelectedDate,"dd/mm/yyyy")
MsgBox Format(SelectedDate,"dd/mm/yyyy") ' Returns 05/02/2018 - Correct
MsgBox Sheets("CONTROL").Range("F36").Value ' Returns 02/05/2018 - Incorrect
Any help would be greatly apprecaited.
Easy to get confused with Excel, VBA and date formats. Even easier when you are using strings instead of dates in VBA and/or on the worksheet. And it will be easier to figure out what is going on if you format your output to unambiguous dates eg: dd-mmm-yyyy.
You are entering a string into F36. VBA is US Centric and thinks the string represents May 2, 2018
To have the string interpreted the same as your windows short date format, you can use the Datevalue function and treat your entries as dates and not as strings.
So one alternative to your code would be:
Sub marine()
Dim SelectedDate As Date
SelectedDate = DateValue("05/02/2018") 'will convert to 5-Feb-2018
Sheets("CONTROL").Range("F36").NumberFormat = "dd/mm/yyyy"
Sheets("CONTROL").Range("F36").Value = SelectedDate
MsgBox Format(SelectedDate, "dd/mm/yyyy")
MsgBox format(Sheets("CONTROL").Range("F36").Value,"dd/mm/yyyy")
End Sub
The NumberFormat properties just define the way the underlying value is visually represented, but it doesn't change the underlying value itself. Instead of printing the Value property, use Text (which holds the formatted representation of the underlying value):
MsgBox Sheets("CONTROL").Range("F36").Text
Alternatively, if you want to stick on the Value property of the cell, you are forced to print it as follows:
MsgBox Format(Sheets("CONTROL").Range("F36").Value, "dd/mm/yyyy")

Excel 2007 VBA Code- Splitting Cells

Excel 2007- I have countless old Word Tables that I'd like to put into Excel. I'd like to split the contents of the cell into two cells. Most of the cells have a very similar format (I don't need to split the ones without this format)- Text (Date). I've tried using "LEFT" or "RIGHT" but since the text string and date string are variable lengths there are no good straightforward ways. For example-
Cell A1-
"Market Value (6/16/09)" [or "Addition (12/15/09)", etc.]
I'd like to split the cell into-
Cell A1- "Market Value" and
Cell B1- "6/16/09"
Obviously if it takes the A1 data and puts it into B1/C1 I could care less.
I've seen some other split VBA modules but they don't seem to be doing the trick for me. I've looked for ways to split it using CSV but that doesn't seem to be useful either. So is there a way to use the "(" or ")" as a marker to copy text before or after the "("?
So is there a way to use the "(" or ")" as a marker to copy text before or after the "("?
Yes
Example
Cell A1- "Market Value (6/16/09)"
Sub Sample()
Dim Ar() As String
Ar = Split(Range("A1").Value, "(")
Debug.Print Ar(0) '<~~ This will give Market Value
Debug.Print Ar(1) '<~~ This will give 6/16/09)
'<~~ And the below will give you 6/16/09
Debug.Print Replace(Ar(1), ")", "")
End Sub

Format cell for comma-separated values

Written a VBA script which outputs numbers and I thought I had the correct format string for thousand-separating (4,656,565 5,343 232,434 etc) but its not working for certain magnitudes of numbers.
So far I am using Cells(x,y).NumberFormat = "#,###"
can someone provide me with the correct format string to thousand-comma-separate any number, no matter the magnitude?
This works for me. Notice, formatting the cell first before assigning the number to it
Option Explicit
Sub Sample()
With Cells(1, 1)
.NumberFormat = "#,##0"
.Value = 4.65656553432324E+16 '46565655343232400
End With
End Sub
RESULT
Cell A1 has 46,565,655,343,232,400

Resources