Another date format problem in VBA

Yamezz

Active Member
Joined
Nov 22, 2006
Messages
367
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

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
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,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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