Consulting

Results 1 to 4 of 4

Thread: VLookup, how to reverse columns?

  1. #1
    VBAX Contributor
    Joined
    Jun 2006
    Posts
    135
    Location

    VLookup, how to reverse columns?

    Hi,

    According to the VBA help, you have to look for a value in the first column of your range with VLookup.

    I need to look a value in column A and find the corresponding value in column B, which I do thanks to:

    [VBA]Application.VLookup(string, Columns("A:B"), 2, 0)[/VBA]

    Then I need to do it the otherway, and the only way I found yet is to copy column A in column C and use

    [VBA]Application.VLookup(string, Columns("B:C"), 2, 0)[/VBA]

    Is there a way to do it directly looking for a value in column B and taking the value in front in column A using Vlookup?

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Quote Originally Posted by jungix
    Is there a way to do it directly looking for a value in column B and taking the value in front in column A using Vlookup?
    No, so try
    [VBA]
    Sub lookup()
    Dim a
    Dim str As String
    str = "Test28"
    Set a = Range([A1], [A1].End(xlDown))
    Set b = Range([B1], [B1].End(xlDown))
    MsgBox a(Application.WorksheetFunction.Match(str, b, 0))
    End Sub

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Expert
    Joined
    Jul 2004
    Location
    Wilmington, DE
    Posts
    600
    Location
    You can do:

    [VBA]
    With Application
    MyVar = .Index([a:a], .Match(LookFor, [b:b], 0), 1)
    End With
    [/VBA]
    Regards,

    Patrick

    I wept for myself because I had no PivotTable.

    Then I met a man who had no AutoFilter.

    Microsoft MVP for Excel, 2007 & 2008

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Neat
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads '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
  •