Keep Leading Zero in External File Reference

jswhip

Board Regular
Joined
Jan 19, 2005
Messages
65
Long story short, automating VBA code so I can reference previous files without having to edit the code. The file names are based off the current date. All works well when it's a two digit month, but single digits get lost in the translation.

Here is the code: Range(Cells(2, 5), Cells(FinalRow, 5)).FormulaR1C1 = "=IFERROR(VLOOKUP(RC1,<directory (excluded for clarity)> [" & MonthNum & "-" & YearNum & " Final Pricing.xlsb]Final'!C1:C17,13,FALSE),"""")"

I've tried to format MonthNum (e.g. MonthNum = Format(MonthNum, "00"), plus multiple other formatting variants, but this throws an error. I'm assuming it's because MonthNum is seen by VBA as a string after formatting, thus need some combination of ampersands and quotes, but I can't get it to work. If I don't format MonthNum, the VBA code works, but the file isn't found (it's saved as mm-yy with leading zeroes).

Thoughts?
 

Some videos you may like

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.

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
6,714
Declare MonthNum as a Variant or a String and it can be a number or a formatted string:
VBA Code:
    Dim MonthNum As Variant 'or String
    MonthNum = 5
    MonthNum = Format(MonthNum, "00")
    Debug.Print MonthNum
 

jswhip

Board Regular
Joined
Jan 19, 2005
Messages
65
Thanks.

The problem isn't in the conversion (that's the easy part). It gets converted just fine.

It's using it in the formula. If I exclude the format line you mentioned, the formula works (but can't find the file as it has a leading zero). But, if I use the format line, I get the ever helpful "Run-time error '1004': Method 'Cells of Object'_global failed".

I'm surmising since MonthNum is a text string, that I need quotes/ampersands, but that's where I get stuck.
 

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
6,714
Two things about your formula don't look right.

Is RC1 an A1 cell reference, or is it meant to be R1C1 notation? If the latter then it is neither a relative nor absolute reference.
The VLOOKUP is looking in a single column array C1:C17, yet you are asking for the 13th column of that array.
 

jswhip

Board Regular
Joined
Jan 19, 2005
Messages
65
Thanks for responding.

The formula is populated in a defined range. Using the RC1 in this manner allows the formula to populate so each cell reference in the row resolves to that row. The alternative is I could set up a For/Next loop, but this is much more efficient.

I use the C1:C17 as I use the formula in a few other places, but reference different columns. Helps keep the maintenance down if I ever need to change anything.

Again, the issue is how the text formatted value throws an error, but the unformatted doesn't. I'm at a loss to resolve.
 

Watch MrExcel Video

Forum statistics

Threads
1,127,528
Messages
5,625,339
Members
416,096
Latest member
forevans

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