NOW function not working

Highlighted
Occasional Visitor

When i use NOW function in worksheet it works normally returns the system date and time but when I use the same now function in VBA it swaps the values of dd and mm.

1 Reply
Highlighted

Hi @hitnvhd 

 

Date & time format are return based in on your system date & time settings, pls check the same it will be as DD/MM/YYY HH:MM. If you don't wish to disturb your system setting then use the format function to format the date.

 

Declare the variable a as Date instead of string.

 

Without formatting 

Private Sub CommandButton1_Click()

Dim a As Date

a = Now

ActiveSheet.Range("b2").Value = a

End Sub

 

With Formatting 

Private Sub CommandButton1_Click()

Dim a As Date

a = Now
ActiveSheet.Range("b2").Value = Format(a, "MM/DD/YYY HH:MM")

End Sub

 

Regards, Faraz Shaikh | MCT, MIE, MOS Master, Excel Expert

If you find the above solution resolved your query don't forget mark as Official Answer to help the other members find it more