I'm writing a Macro where I need two things in one of the column ranges:
a) Cell values must be the date of the next day in the dd/mm/yyyy format;
b) Cell Format must be text
I keep running into the problem of the date format being changed to mm/dd/yyyy.
Default excel format for the vba function date is dd/mm/yyyy ,since my excel language is set to Portuguese.
By running:
Sub format()
Range("A1").Value = Date + 1
End Sub
I get the value 11/02/2023, date format, which is expected. But if I run:
Sub format2()
Range("A1").NumberFormat = "#"
Range("A1").Value = Date + 1
End Sub
The output is 02/11/2023 in text, in the mm/dd/yyyy format. I'd like to understand why this would even happen
Same thing happens for me. Not sure why but you can achieve what you want by doing:
Sub format2()
Range("A1").NumberFormat = "#"
Range("A1").Value = format(Date + 1, "dd\/mm\/yyyy")
End Sub
This will result in the cell being formatted as text in format "dd/mm/yyyy".
If you wanted the same format but actually formatted as date, you should do this:
Sub format2()
Range("A1").NumberFormat = "dd\/mm\/yyyy;#"
Range("A1").Value = Date + 1
End Sub
The backslashes are there because excel (at least on my system) considers the forward slash a substitute for default separator and replaces it with hyphens, which are my actual default separators.
I'd like to credit this answer for the last point (escaping the forward slashes): https://stackoverflow.com/a/8527762/9852011
Related
The VBA built-in Date variable can give me today's date (8/25/21 as the time this post is written). However, I really want the date in the mm/dd/yyyy format for future projects and store it in a variable - myDate. I just couldn't think of an easy string manipulation solution to get the desired result.
I've tried put the Date variable in excel sheets and then change the number format using the below code:
[A1]=Date
[A1].NumberFormat="mm/dd/yyyy"
myDate=[A1].value
debug.print myDate
Even though the number format code will change the appearance of cell [A1], making it look like it's in the desired format mm/dd/yyyy, the debug.print line still gives 8/25/21 instead of 08/25/2021
A date value holds no format. Apply the format for display - that includes when calling Debug.Print:
Dim myValue As Date
[A1] = Date
[A1].NumberFormat = "mm/dd/yyyy"
myDate = [A1].Value
Debug.Print Format(myDate, "mm/dd/yyyy")
I was trying to import a CSV file to my phpmyadmin. Then my date field had values as 0000-00-00 since both date formats were different. I changed the CSV date-format to the format in the database. I have 5000+ data in my CSV. but only a few dates change to the format I chose. Remaining still in the old format
I went to Format cells, selected date, changed to Uk and selected the desired date format. But only a few dates change to the format I chose. Remaining still in the old format
Starting with data like:
Select the cells you wish to convert and run this:
Sub FixDate()
Dim s As String, d As String, rng As Range, cell As Range
Dim dt As Date
d = "-"
For Each cell In Selection
s = cell.Text
If InStr(s, d) > 0 Then
arr = Split(s, d)
dt = DateSerial(arr(2), arr(1), arr(0))
cell.Clear
cell.Value = dt
cell.NumberFormat = "dd-mm-yyy"
End If
Next cell
End Sub
to produce:
You can only change the date format in Excel once Excel recognizes the cell contents as dates. If this doesn't happen automatically, you can force it with the Text to columns function. After the dates have been recognized, you can use any date format you like.
Select the column with the unrecognised dates, and go to Data tab, click Text to columns.
Select Delimited, Next, Untick everything, Next, Select Date and from the dropdown select the order in which the dates currently are in your column. Hit Finish.
In my example dates are in yyyy-dd-mm format.
This is the fastest way I know of in Excel to make it recognize dates in any order.
I have a simple request, to paste the data exactly as visible in Excel.
I have a list of dates in mm/yyyy format, but Excel keeps adding mm/dd/yyyy which is throwing off my analysis. It's formatted to show simply mm/yyyy but the actual cell value keeps getting set to mm/01/yyyy.
How can I simply copy/paste the value to be mm/yyyy.
I've tried Range("A1").Value = Range("A1").Value, but of course that just keeps the same info.
Yes, in my case since it's dates, I can do a kludgy function that takes the left three characters, and combines with the rightmost four. However, that really just gets the date number returned. I tried on G4 and get 4171730. Plus, I'd like to know how to do this with other types of cell values too (strings, numbers, etc.).
save the value and the format then set the cell as text and assign the formatted value:
Sub test()
Dim t As Variant
t = Range("A1").Value2
Dim x As String
x = Range("A1").NumberFormat
Range("A1").NumberFormat = "#"
Range("A1").Value = Format(t, x)
End Sub
This also works
Sub test()
Dim t As String
t = Range("A1").Text
Range("A1").NumberFormat = "#"
Range("A1").Value = t
End Sub
Range("A1").Value = Format(yourdate, "mm/yyyy")
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")
I have some dates in a string in a column.
Because the format seems to be M/DD/YYYY I sometimes get a VALUE error when using DATEVALUE.
How can I convert a column to make sure all dates are correct.
For example, the following 2 cells
9/29/2006 12:49:58.956 AM DATEVALUE gives an error
9/12/2008 5:36:59.356 PM DATEVALUE converts to 39791
You need to select the range (just one colunm) with the dates... Go to:
(in Excel 2010)
Data >>> Text to Columns
Inside the dialog box select:
Delimited >>> Next
Just select Tab checkbox.
And heres is the magic!
You need to define the arrangement for your dates...
In the Date field:
Choose the what you need (as you say in your question is M/D/Y)
Destination field:
Make sure that is the same of the data you want to format.
And finish.
The problem you are having is probably that the data is getting interpreted as a general field, and shows up as, say 49:59.0 - which isn't text, which is what Datevalue() expects as input; for both inputs, Datevalue(CellID) words if you prepend the text with a ' - which ensures it is treated as text.
it seems that your system settings use dd/mm/yyyy for short date format. and datevalue function uses this settings, so its try to read the day from the first part of the string and the month from the second part. as in your example 9/29/2006 12:49:58.956 AM there is no month with 29 then it gives error, and in the second example it gives 39791 = 9 december 2008 and NOT 12 september 2008.
If you change the short date format in your system settings to mm/dd/yyyythen datevalue function will work correctly, but this not acceptable, so we need to replace month with day and day with month to get correct date.
I don't know if we can do this with Excel formula but we can do that simply with VBA.
So try this code:
Sub Test()
Dim d1 As Variant, d2 As Variant, td As Date
Dim Rng As Range, CL As Range
Set Rng = Range("A1:A2") ' change this to your range
For Each CL In Rng
d1 = Split(CL.Value, " ")
d2 = Split(d1(0), "/")
td = DateSerial(d2(2), d2(0), d2(1))
CL.Offset(0, 1) = td
CL.Offset(0, 1).NumberFormat = "mm/dd/yyyy" ' change the Date format as you need
'CL.Offset(0, 1).NumberFormat = "dd/mm/yyyy"
Next
End Sub