PDA

View Full Version : Solved: Using Variable Col_Index with VLOOKUP



ranuse
12-13-2010, 09:38 AM
Hi,
I'm having problems displaying the value of the variable X in the VLOOKUP function. After executing the code, the letter "X" appears in the VLOOKUP forumla instead of the actual value of X.

I'm also having problems with the 'Sheet1'!A:Z portion of the formula. I want the table_array to be from columns A to Z, but after executing the macro, the formula turns out to be 'Sheet1'!A:(Z).


Sub Macro1()
'
' Macro1 Macro
'
Range("I22").Select

Dim X As Long
Dim Row As Long, Col As Long

X = 2
Row = 22
Col = 3

Do Until Col > 7
Cells(Row, Col).Select
ActiveCell.FormulaR1C1 = _
"=VLOOKUP(""Test"", 'Sheet1'!A:Z, X, FALSE)"
Col = Col + 1
X = X + 1
Loop
'
End Sub

Tinbendr
12-13-2010, 09:48 AM
You'll have to take the X out of the quotes.

"=VLOOKUP(""Test"", 'Sheet1'!A:Z, X, FALSE)"
"=VLOOKUP(""Test"", 'Sheet1'!A:Z, " & X & ", FALSE)"

ranuse
12-13-2010, 10:29 AM
Thanks, it's working. SOLVED