Jul 04 2020 12:16 AM
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.
Jul 05 2020 04:26 AM
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