Format function not working in excel vba - excel

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"

Related

Date String manipulation in Excel VBA

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")

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!

Change String date (DD.MM.YY) to Dateformat(YYYY-MM-DD) in excel

I have nearly 2,00,000 records of data in my excel sheet and I have Date columns as a string like this (dd.mm.yy) I have to convert it into dateformat of (YYYY-MM-DD) .
I tried this -
For the example date string, 1.11.11 , I tried to replace .11 with .2011 but it will also change the month to 1.2011.2011 and my sheet has huge records from 2010 to 2016 .
Example - My cell has this 1.11.11 and i want to convert it to 2011-01-01 (YYYY-MM-DD)
Please help !
Thank you.
Use this to change it to a true date:
=DATE(2000+SUBSTITUTE(RIGHT(A1,2),".",""),SUBSTITUTE(MID(A1,FIND(".",A1)+1,2),".",""),SUBSTITUTE(LEFT(A1,2),".",""))
Then format the output to your desired: YYYY-MM-DD
To do it in place one would need vba.
Select your date cells and run this code.
Sub datechange()
Dim rng As Range
Dim str() As String
For Each rng In Selection
str = Split(rng.Value, ".")
rng.Value = DateSerial(2000 + str(2), str(1), str(0))
Next rng
Selection.NumberFormat = "yyyy-mm-dd"
End Sub
Assuming the values start in A1 AND if they are all truly in the DD.MM.YY (with zeroes (04, 06, etc...)
="20"&RIGHT(A1,2)&"-"&MID(A1,4,1)&"-"&LEFT(A1,2)
Try bellow formula :
=TEXT(SUBSTITUTE(A1,".","/"),"YYYY-MM-DD")
Try this:
select convert(varchar,convert(datetime,'26-July-2018'),105)

Date format is switching month and day

Could you please someone can help me on how to change the date formatting cause i use the following code and instead of today's date it gives me august date. I mean it takes the date of my laptop (08-sep-2015) and it returns on the excel cell (09-Aug-2015).
Private Sub UserForm_Initialize()
'testing variable
'Dim LValue As String
'LValue = Format(#4/17/2004#, "Long Date")
'close testing variable
Me.tbDate = Date
'fill combobox
For Each cell In [cartridges]
Me.cmbCartridges.AddItem cell
Next cell
End Sub
The part that i exclude form the code is a test i made for a solution i found over the internet.
Also the text box that i want to present the date has the following code
ssheet.Cells(nr, 1) = CDate(Me.tbDate)
I found this and sorted my issue. Now everything its working like a charm.
Me.tbDate = Format(Date, "dd/mm/yyyy")
I resolved my issue with this code. I intentionally formated the cell the wrong way (excel default) and then formated it as I needed.
For j = 0 To LISTBOXNAME.ListCount - 1
LISTBOXNAME.List(j) = Format(DateValue(LISTBOXNAME.List(j)), "m/d/yyyy")
LISTBOXNAME.List(j) = Format(DateValue(LISTBOXNAME.List(j)), "dd.mm.yyyy")
Next j

Wrong VBA date format in Excel cell

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"

Resources