Keep leading zero

zJenkins

Board Regular
Joined
Jun 5, 2015
Messages
148
I have some vba code that saves a file to a specific folder based on the month. The month is set by the user as a two digit format (01, 02, etc) and set as a variable in my vba code. However, when I run the code the file doesn't save, and I've found that it has to do with the fact that the variable is only holding one digit when the month is < 10, in other words, it drops the leading 0.

What code can I use to keep the leading 0?

Code:
 Workbk2.SaveAs Filename:= "G:\Accounting\HotShop Reports\" & rptYear & "\00-Hotshop Critique\" & rptMonth & "\HS_Critique_" & username3 & ".xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False

The formula I'm using for the date is:

=TEXT(A1,"mm")

(A1 contains the date)
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Untested but maybe this:
Code:
Workbk2.SaveAs Filename:="G:\Accounting\HotShop Reports\" & rptYear & "\00-Hotshop Critique\" & [COLOR=#0000ff]Format(rptMonth, "00")[/COLOR] & "\HS_Critique_" & username3 & ".xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
 
Upvote 0
The month is set by the user as a two digit format (01, 02, etc) and set as a variable in my vba code.
Akuini's suggestion may well suit you but I'm wondering
- How is that variable declared in your code?
- How is that variable populated in your code?

As a guess, something like this should hold the leading zero if cell A2 is holding your =TEXT(A1,"mm") formula
Code:
Dim rptMonth As String

rptMonth = Range("A2").Text
 
Upvote 0

Forum statistics

Threads
1,224,815
Messages
6,181,135
Members
453,021
Latest member
Justyna P

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