VBA to move current date into cell

Shadkng

Active Member
Joined
Oct 11, 2018
Messages
365
Hi, I am having trouble moving the current date into a cell. When I use the below line I am getting a funny date, 4/45/27. When I manually change the format to date then the it corrects.
VBA Code:
Worksheets("COPYORD").Range("D" & y).Value = Date

The I added the below line to change the format, but I got a "false" return.
VBA Code:
Worksheets("COPYORD").Range("D" & y).Value = NumberFormat = "dd-mm-yy"

I have been searching online for an answer, but I must be missing something.

Thanks
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Your first code line is fine (assuming there is a sheet named COPYORD or that the variable y has been assigned a value. I have no explanation as to the value you showed us. I would suggest creating a new workbook, rename a sheet to COPYORD and then try your code in that new workbook and see if it works correctly or not.

As to your second code line, that syntax is incorrect. NumberFormat is a property of the Range object so you don't assign it to the Range's Value, That code line should look like this...

Worksheets("COPYORD").Range("D" & y).NumberFormat = "dd-mm-yy"
 
Upvote 0
Your first code line is fine (assuming there is a sheet named COPYORD or that the variable y has been assigned a value. I have no explanation as to the value you showed us. I would suggest creating a new workbook, rename a sheet to COPYORD and then try your code in that new workbook and see if it works correctly or not.

As to your second code line, that syntax is incorrect. NumberFormat is a property of the Range object so you don't assign it to the Range's Value, That code line should look like this...

Worksheets("COPYORD").Range("D" & y).NumberFormat = "dd-mm-yy"
OK, so I took the variable out of the equation and I still get the wrong date. If I change the cell to D1, it works correctly. I guess there could be something elsewhere in the code changing that cell format.

VBA Code:
Worksheets("COPYORD").Range("D5").Value = Date
        Worksheets("COPYORD").Range("D5").NumberFormat = "dd-mm-yy"
 
Upvote 0
After the cell gets the wrong date, look up the format for the cell... look up the Custom format and write back to us what it says there.
 
Upvote 0

Forum statistics

Threads
1,214,924
Messages
6,122,293
Members
449,077
Latest member
Rkmenon

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