Date value in VBA appears as mm/dd/yyyy instead of the actual format which is mm-dd-yyyy

bellg

New Member
Joined
May 13, 2014
Messages
29
Hi,

I can't get my my date to display in the format that I want it to as a string variable in vba.

Code:
Public Sub Todaydate()
    Today = Sheets("Date").Range("B1").Value
    MsgBox Today
End Sub

The msgbox displays 3/21/2017
The value in B1 shows as 3-21-2017

I want the variable "Today" to use the -'s so that I can use it to save in a filename. Does anyone know how to get VBA to display the -'s instead?

I even tried using some left, mid, right commands to pull the dates, but it doesn't seem to recognize the date format that the cell is saved into and always defaults to m/d/yyyy, so that doesn't work very well. I could add some if statements for months/days that are two digits, but I was curious if anybody knew why it defaults to /'s and how to avoid that.
Code:
Public Sub Todaydate()
    Today = Left(Sheets("Date").Range("B1").Value, 2) & "-" & Mid(Sheets("Date").Range("B1").Value, 4, 2) & "-" & Right(Sheets("Date").Range("B1").Value, 4)
    MsgBox Today
End Sub
 
Hi,
If you just want to return what you see in the cell rather than it's underlying value then try using the Range.Text property & see if this solves your problem.

Rich (BB code):
Today = Sheets("Date").Range("B1").Text

Dave
 
Upvote 0
Hi,

I can't get my my date to display in the format that I want it to as a string variable in vba.

Rich (BB code):
Public Sub Todaydate()
    Today = Format$(Sheets("Date").Range("B1").Value, "m-d-yyyy")
    MsgBox Today
End Sub
What happens if you make the change to your code that I show in red above?
 
Upvote 0

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