PDA

View Full Version : Using a Variable in a V-Look up formula



Djblois
05-24-2007, 01:20 PM
I am trying to use a variable in a v-lookup formula, is this possible?

Instead of this:
Sub comAddWhse(ByVal rngToAdd As Range)

rngToAdd.FormulaR1C1 = _
"=VLOOKUP(RC[-1],'[BusinessReportingReference.xls]Whses'!C1:C8,2,FALSE)"
rngToAdd.Value = rngToAdd.Value
End Sub

I want to use this code:

Sub comAddWhse(ByVal rngToAdd As Range, ByVal intColumnDiff As Integer)

rngToAdd.FormulaR1C1 = _
"=VLOOKUP(RC[intColumnDiff],'[BusinessReportingReference.xls]Whses'!C1:C8,2,FALSE)"
rngToAdd.Value = rngToAdd.Value
End Sub

right now it just crashes, is there another way to get the same effect? The reason why I am doing this is I use that formula multiple times and each time the Column difference is different.

Bob Phillips
05-24-2007, 01:57 PM
Sub comAddWhse(ByVal rngToAdd As Range, ByVal intColumnDiff As Integer)

rngToAdd.FormulaR1C1 = _
"=VLOOKUP(RC[" & intColumnDiff & "],'[BusinessReportingReference.xls]Whses'!C1:C8,2,FALSE)"
rngToAdd.Value = rngToAdd.Value
End Sub

vonpookie
05-24-2007, 01:58 PM
Keep the variable name *outside* of the quotes for the formula.

So:
rngToAdd.FormulaR1C1 = _
"=VLOOKUP(RC[" & intColumnDiff & "],'[BusinessReportingReference.xls]Whses'!C1:C8,2,FALSE)"