Format and DateAdd

vba_monkey

Board Regular
Joined
Dec 18, 2013
Messages
112
Hello,

The following code will return the value 20173, can anyone tell how to get it to return the value 201703?

Code:
Format(DateAdd("m", -8, Date), "yyyy") & Format(DateAdd("m", -8, Date), "m")

I don't mean to simply add the zero so if this code was run it should return the value 201710 not 2017010

Code:
Format(DateAdd("m", -1, Date), "yyyy") & Format(DateAdd("m", -1, Date), "m")

Thanks
 
Last edited:

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
You don't need to do it in two parts. Just do this:
Code:
Format(DateAdd("m", -8, Date), "[B][COLOR=#ff0000]yyyymm[/COLOR][/B]")
 
Last edited:
Upvote 0
You are welcome.

Yes, since it is a single date (the same date) you are formatted, you can do it like that, and choose any Date format you like (just as if you were formatting the cell for a date format).
You could have done it your original way to, just choosing "mm" for the second format. But there is no need to break it up, since it is the same date.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,207
Members
452,618
Latest member
Tam84

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