Can't get the Date to format correctly with VBA

BradA

Board Regular
Joined
Sep 24, 2010
Messages
75
Hello,

Each morning we run a report which I am in the process of automating. The one sticking point I have is something that has tripped me up several times in the past: One of the fields that is pulled into the daily report is a date field. It is displayed in Excel in mm/dd/yyyy format, but when you click in one of the date cells and look at the value that's actually in the cell it's in m/d/yyyy format. I want to take the date in mm/dd/yy format and do a couple things with it. First, I want to have it feed into cell B3 such that B3 displays "Data as of (whatever the date is in the report, in mm/dd/yy format)". I am trying to do this in the following way:
Code:
        Range("B3").Value = "Data as of " & Range("W6").Value
        Range("B3").NumberFormat = "mm/dd/yy"
where B3 is where I ultimately want it displayed and where W6 is where the date is currently residing...but I can't get it to display as anything other that m/d/yyyy format. Second, I want to use the date, this time in yyyymmdd format, in the book name. I think I can do that on my own, if I could get it to accept the date in yyyymmdd format.

Any help is greatly appreciated.

Thanks
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
You are assigning a text string to B3, so it is not a date... hence, you cannot format it as a date. I think this single line of code may be what you are after...

Code:
Range("B3").Value = "Data as of " & Format(Range("W6").Value, "mm/dd/yy")

I think you can use the same structure as above for your second question, just use "yyyymmddd" for the second argument to the Format function call.
 
Last edited:
Upvote 0
Hi BradA,

For your first query, try this:

Code:
Range("B3").Value = "Data as of " & Format(Range("W6").Value, "mm/dd/yyyy")

Not too sure about what you're actually after re your second query, but I'd say you'd be able to adapt the above to meet your needs.

HTH

Robert
 
Upvote 0
Rick and Trebor,

Thanks to both of you for your responses. What an easy and wonderful solution. Much appreciated.
 
Upvote 0

Forum statistics

Threads
1,214,808
Messages
6,121,684
Members
449,048
Latest member
81jamesacct

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