Another date format problem in VBA

Yamezz

Active Member
Joined
Nov 22, 2006
Messages
364
Office Version
  1. 2019
I have dateserial values that I need to enter back into a worksheet with the correct day/month/year format.
Example: the CashDate variable is 45419. When formatting this manually to a date in Excel, the correct date of 7th of May 2024 is displayed.
However, if I get VBA to do it with = Format(CashDate, "dd/mm/yyyy") I get 5th of July 2024 in the cell, which is not what I asked for. This is not just a display issue - when I manually check by formatting back to a number in Excel I get 45478.

How do I get 45419 to read 07/05/2024 (the 7th day of May, 2024)?

Someone should really drag America kicking and screaming into the 18th century...
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Hi, just write the variable to the cell without trying to format it, if you specifically need to apply formatting then apply it to the cell and not the variable.
 
Upvote 0
For example:
VBA Code:
Sub Example()
Dim CashDate As Long
CashDate = 45419
With Range("A1")
    .Value = CashDate
    .NumberFormat = "dd/mm/yyyy"
End With
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,892
Messages
6,175,236
Members
452,621
Latest member
Laura_PinksBTHFT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top