Wrong VBA date format in Excel cell - excel

I'm trying to print out different date formats in my Exel sheet using VBA. However, I can't seem to output the "yyyy-mm-dd" format, or the "mmm dd, yyyy" formats. The cells don't seem to print out the correct formats. For instance:
'Declare new date variables
Dim LongDateFmt As String
Dim ShortDateFmt As String
Dim MediumDateFmt As String
Dim NewDateFmt As String
'Change the date to the new submission date
MediumDateFmt = Format(Date, "yyyy-mm-dd")
Range("A1").Value = MediumDateFmt
LongDateFmt = Format(Date, "Long Date")
Range("A2").Value = LongDateFmt
ShortDateFmt = Format(Date, "Short Date")
Range("A3").Value = ShortDateFmt
NewDateFmt = Format(Date, "MMMM dd, yyyy")
Range("A4").Value = NewDateFmt
A1 prints out 13/06/2013, A2 prints 13-Jun-13, A3 prints out 13/06/2013 and A4 prints 13-Jun-13 as well.
Is this a settings problem or is my code wrong?

you ought to change the cell format and not use formatted strings
with Range("A1")
.Value = Date
.Numberformat = "yyyy-mm-dd"
end with
for instance

Format doesn't set the format of a cell to what you want, it simply gives you a string of a specified format. Inserting that string into a cell won't necessarily give you the results you want since the current formatting of the cell may have an impact.
If you want to change formatting of the cell itself, you need to set its NumberFormat property, with something like:
Range("A1").NumberFormat = "yyyy-mm-dd"

Related

Problem with value as date and format as text in cell

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

Change date format from dd/mm/yyy to dd-mm-yyyy

I have tired to change the date format from dd/mm/yyyy to yyyy-mm-dd. In the front end users entered the date in dd/mm/yyyy format while import the same details to Text file the date format should be printed in yyyy-mm-dd format.
Range("A1").Value = Format(Range("A1").value, "dd/mm/yyyy")
Range("A1").Value = Format(Range("A1").value, "dd/mm/yyyy")
The user has entered the date dd/mm/yyyy in Cell A1. While importing the same to Text file it should be printed as yyyy-mm-dd
You should change the formatting of the cell, too, not just its value:
Range("A1").NumberFormat = "yyyy-mm-dd"
If your date in A1 is a string/text (and not a real date) …
… you cannot use Format() which only works on real dates and numbers. Therfore you need to analyze the string and create a real date first:
Dim DateString As String
DateString = Range("A1").Value
'split string into 3 parts
Dim DateArr As Variant
DateArr = Split(DateString, "/")
'create a real date out of the 3 parts
Dim RealDate As Date
RealDate = DateSerial(DateArr(2), DateArr(1), DateArr(0))
'write real date into cell
Range("A1").Value = RealDate
'format it in desired format
Range("A1").NumberFormat = "yyyy-mm-dd"
If your date in A1 is a real date (not a string) …
… then you can just format your cell
Range("A1").NumberFormat = "yyyy-mm-dd"
Recommendation
Never use strings for dates. A date has always to be a date format never a string/text. Otherwise you cannot tell if a date is dd/mm/yyyy or mm/dd/yyyy. String dates are evil!

Format function not working in excel vba

I am new to vba. i am trying to convert current date format to mm/dd/yyyy. I used format function to achieve it. Still i am getting as dd-mm-yyyy format.
Dim current as Date
currentDate = Format(Now, "MM/dd/yyyy")
Thanks in advance.
This is because Format() returns a String:
Sub NeedADate()
Dim s As String
s = Format(Now(), "mm/dd/yyyy")
MsgBox s
End Sub
First paste the desired date in a cell, then format the cell layout.
Cells(1,1).Value = Now()
Cells(1,1).Numberformat = "mm/dd/yyyy"

Excel Date Issue

Using Excel 2003 VBA code, I copied a date from a form into a cell, which I then formatted as vbLongDate. This puts in a date like "Wednesday, August 13, 2014". I am struggling with getting the date components back out, though. I am trying this
Dim myDate As Date
myDate = Date(Range("A1").Value)
From there, I want to pull out the year, month and day values from myDate, but this isn't working. I tried a few variations, but they are all bombing out.
Thanks!
I assume you use the FormatDateTime() function with vbLongDate. That returns text. Consider formatting the cell with a date format instead and retain its date properties.
Sub test()
Dim myDate As Date
' this will return text
Range("A1") = FormatDateTime(Now, vbLongDate)
' A2 will remain a date
Range("A2") = Date
Range("A2").NumberFormat = "[$-F800]dddd, mmmm dd, yyyy"
myDate = Range("A2").Value
Debug.Print Month(myDate) ' no error
End Sub
Edit: Depending on the date format used, the appearance of the date will adjust to the regional settings.
' this date format will adjust to the regional settings
' my regional settings are DMY and I see "Thursday, 14 August 2014"
' even though the format shows the mmmm before the dd
myRange1.NumberFormat = "[$-F800]dddd, mmmm dd, yyyy"
' this date format will always be like displayed below
myRange2.NumberFormat = "dddd, d mmmm yyyy;#"
You could set different formats to different cells, depending on whether they need to adjust or not.

How to COUNTIFS with cells formatted as date

Excuse me, again about COUNTIFS
Dim a As String
a = ct.Range("B4").Text ' B4 is formatted as date
Range("C6").Value = Application.WorksheetFunction.CountIfs(rDat, a, rSec, "1")
rDat and rSec are defined ranges (on another sheet).
rDat has the same format as ct.B4 (i.e. date)
I got "0" as a result, but it is wrong.
I tried to remove date format, and format as text - it works.
But I need date format in rDat and B4.
Dim a As Date
a = ct.Range("B4") ' do not convert B4 to text
Range("C6").Value = Application.WorksheetFunction.CountIfs(rDat, a, rSec, "1")

Resources