Another date format problem in VBA

Yamezz

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

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
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,217,371
Messages
6,136,170
Members
449,996
Latest member
duraichandra

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