Converting mm/dd/yyyy to mm-dd-yyyy

pikt11

New Member
Joined
Jun 10, 2019
Messages
6
Hi everyone,

I've been stumped by this for a while now: when I try to convert a date value form mm/dd/yyyy to mm-dd-yyyy, and output it as a message box, the code works perfectly. However, when I try to put this value into a cell, it reverts back to the original formatting of mm/dd/yyyy. I know this probably has something to do with maybe the default format setup of the worksheet, but I don't know how to fix it.

Thanks in advance, working with dates in VBA has always resulted in a ton of frustration for me :(

Code:
 myfiledate = format(dateresult, "mm-dd-yyyy")
 MsgBox myfiledate
 Range("Z1").Value = myfiledate
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Either try using DateValue (or CDate) or put dateresult in the cell rather than myfiledate, presuming of course dateresult is a date value.
Code:
 Range("Z1").Value = DateValue(myfiledate)

Code:
 Range("Z1").Value = dateresult
Now you should have a 'real' date value in the cell and you can format it as you wish.
 
Upvote 0
In Excel the Value is the value regardless of format.
If the date is a text string than it is yet to be converted to a value.

Your snippet doesn't really indicate where you are getting "dateresult"
Once you format the cell to display a date (serial number) as "mm-dd-yyyy" it will be displayed as desired.
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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