VBA Formatting a date is swapping the year for the day.

RockandGrohl

Well-known Member
Joined
Aug 1, 2018
Messages
671
Office Version
  1. 2010
Platform
  1. Windows
Hello, I have a date set as "mindate" which is a long, the serial is 43467 which is 02/01/2019 (2nd Jan)

I'm then turning this into the first day of that month using dateserial

And finally, pasting it in sequence, iterating up one month.

VBA Code:
RBD.Activate
LastrowRBD = Cells(Rows.Count, "A").End(xlUp).Row
mindat = CLng(Application.WorksheetFunction.Subtotal(5, Range("B2:B" & LastrowRBD)))
mindat = DateSerial(Year(mindat), Month(mindat), 1)
maxdat = DateSerial(Year(Date), 12, 31)
x = DateDiff("m", mindat, maxdat)

Rarise.Activate
Range("C3").Activate
y = 0
Do Until y = x
ActiveCell.Value = Format(DateAdd("m", y, mindat), "mmmm-yy")

y = y + 1
ActiveCell.Offset(0, 1).Activate
Loop

What's happening is that if I include the format section, for some reason it's formatting in a weird way and I'm getting "19/01/21"

When I remove format, it goes in perfectly as 01/01/2019, 01/02/2019 etc all the way up to 01/12/2021 (35 iterations)


I'd like to have those dates display neatly as Jan-19, Feb-19, Mar-19 etc.

Thanks.
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
60,150
Office Version
  1. 365
Platform
  1. Windows
Note that the "FORMAT" function in VBA will return a Text value, not a Date one, which will probably mess up your calculations, since you are looping.
I think what you want to do is to paste the value, and then format the cell with your desired format, i.e.

Replace:
VBA Code:
ActiveCell.Value = Format(DateAdd("m", y, mindat), "mmmm-yy")
with
VBA Code:
ActiveCell.Value = DateAdd("m", y, mindat)
ActiveCell.NumberFormat = "mmmm-yy"
 
Solution

RockandGrohl

Well-known Member
Joined
Aug 1, 2018
Messages
671
Office Version
  1. 2010
Platform
  1. Windows
Oh right. Super. Thanks for that. Guess it's another one of those little intricacies to know.
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
60,150
Office Version
  1. 365
Platform
  1. Windows
Oh right. Super. Thanks for that. Guess it's another one of those little intricacies to know.
You are welcome.

The "FORMAT" function in VBA is very similar to the "TEXT" function in Excel.
They both return text values (otherwise, things like leading zeroes would be dropped).
 

Forum statistics

Threads
1,148,334
Messages
5,746,154
Members
423,994
Latest member
blzxatly

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