date formating issue

Michaelpfreem

Board Regular
Joined
Mar 14, 2008
Messages
92
Dear all,

I am having a small formating issue, at least I assume it is a formatting issue.

I have a macro that opens a file each day.

The file name will change each month for example

filename 2011 Apr endoffilename.xls

Next month it will be

filename 2011 May endoffilename.xls

Below is the section of the macro that sorts out the filename:

OldDate = Now
OldMonth = Month(OldDate)
OldYear = Year(OldDate)
If Len(OldMonth) < 2 Then OldMonth = "0" & OldMonth
NewMonth = Format(OldMonth, "mmm")

The year works fine, but the month doesn't.

It finds the OldMonth as "4", I then add a zero so the OldMonth is "04", but I need it to turn that to a text, so in this case the "04" would become NewMonth = "Apr". But instead when it changes it to the three diget version it just becomes "Jan".

Can anyone tell me what I am doing wrong?

Many thanks in advance,
Mike
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Hi Mike

If the Year and Month portion is purely dependent on the current date (I wasn't clear if this was the case) then you could just use Format to generate your string:

Code:
NewFilename = "filename " & Format(Date,"yyyy mmm ") & "endoffilename.xls"
 
Upvote 0
Code:
Sub Foo()
OldDate = Now[COLOR="Red"]()[/COLOR]
OldMonth = Month(OldDate)
OldYear = Year(OldDate)
If Len(OldMonth) < 2 Then OldMonth = "0" & OldMonth
NewMonth = Format(DateSerial(OldYear, OldMonth + 0, 1), "mmm")
MsgBox "Your New Month is " & NewMonth
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,289
Members
452,902
Latest member
Knuddeluff

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