Keep Leading Zero in External File Reference

jswhip

Board Regular
Joined
Jan 19, 2005
Messages
69
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?
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
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
 
Upvote 0
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.
 
Upvote 0
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.
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,213,583
Messages
6,114,506
Members
448,575
Latest member
hycrow

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