Excel shows wrong date format in cell

syzer

New Member
Joined
May 11, 2024
Messages
12
Office Version
  1. 2021
Platform
  1. Windows
Dear guys,

I would like to input through the VBA in the cell the current date with format "dd/mm/yyyy", however finally in the cell shows up in changed format ("mm/dd/yyyy") and I dont know the reason why and how can I solve it.
1730453323235.png

As you can see the variable is defined as Date and until the exact row the variable value is proper, only after writing in the cell is changing.

Refer the highlighted rows.
DateIssueVBA.jpg


Thank you in advance!
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
As you can see the variable is defined as Date

As it is nearly pantomime season the reply from programmers would be, “Oh No Its Not!

In your post, Only the LogDate variable is Date datatype NowDate is Variant data type.

When declaring variables each variable requires its data type to be specified even when it is on the same line otherwise it will be its default type of variant.

Your variables should be declared like this

VBA Code:
   Dim NowDate As Date, LogDate As Date

Also, by using Format in the manner shown, the date will be a string.

To resolve your issue you can try following & see if works for you

VBA Code:
NowDate = Now()


With ActiveSheet.Range("K" & RowNo)

.Value = NowDate

.NumberFormat = "dd/mm/yyyy"

End With


Dave
 
Upvote 0
Solution
As it is nearly pantomime season the reply from programmers would be, “Oh No Its Not!

In your post, Only the LogDate variable is Date datatype NowDate is Variant data type.

When declaring variables each variable requires its data type to be specified even when it is on the same line otherwise it will be its default type of variant.

Your variables should be declared like this

VBA Code:
   Dim NowDate As Date, LogDate As Date

Also, by using Format in the manner shown, the date will be a string.

To resolve your issue you can try following & see if works for you

VBA Code:
NowDate = Now()


With ActiveSheet.Range("K" & RowNo)

.Value = NowDate

.NumberFormat = "dd/mm/yyyy"

End With


Dave
Thank you for your proper explanation and your support. It's helped.
 
Upvote 0
You are welcome glad we were able to help resolve your issue & appreciate your feedback

Dave
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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