Consulting

Results 1 to 3 of 3

Thread: Solved: Using Variable Col_Index with VLOOKUP

  1. #1
    VBAX Regular
    Joined
    Oct 2010
    Posts
    14
    Location

    Solved: Using Variable Col_Index with VLOOKUP

    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'!AZ).

    [VBA]
    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[/VBA]

  2. #2
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location
    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)"

  3. #3
    VBAX Regular
    Joined
    Oct 2010
    Posts
    14
    Location
    Thanks, it's working. SOLVED

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •