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
 

Some videos you may like

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
36,675
Office Version
  1. 2010
Platform
  1. Windows
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:

Trebor76

Well-known Member
Joined
Jul 23, 2007
Messages
4,714
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
 

BradA

Board Regular
Joined
Sep 24, 2010
Messages
75
Rick and Trebor,

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

Watch MrExcel Video

Forum statistics

Threads
1,122,207
Messages
5,594,840
Members
413,944
Latest member
3xc3ln00b

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
Top