Consulting

Results 1 to 5 of 5

Thread: How to construct VLOOKUP in VBA?

  1. #1
    Quote Originally Posted by Simon Lloyd
    You should supply your code or workbook as you probably want a CASE statement, however you asked for VLOOKUP so...[vba]Dim MyVar As String
    Dim MyVarResult
    MyVarResult = Application.WorksheetFunction.VLookup(MyVar, Sheets("Sheet2").Range("A1:A249"), 2, False)
    If MyVarResult <> "" Then
    'DO SOMETHING
    Else
    'DO SOMETHING ELSE
    End If[/vba]MyVar would be your variable. Take a look at Case in conjunction with the worksheet selection_change event.
    Hi Simon,

    It seems that you have some experience with VLookup in VBA, which I have not.

    I tried to include the following statements in a Sub:

    Dim Konti as Range
    Set Konti = Range("Konto_Col")
    KontoCol = Application.WorksheetFunction.VLookup(Kontonr, Konti, 2, False)

    "Konto_Col" is a 2 column range in the worksheet. "Kontonr" is passed to the sub as an argument.

    When running the sub I get the following error message (translation Danish/English might not be exact):

    Run time error '1004': Cannot use the property VLookup for the class WorksheetFunction.

    Can you or somebody else tell, what is wrong?

    Best regards

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    This works for me
    [VBA]
    Option Explicit

    Sub Test()
    Dim k
    k = 5
    MsgBox KontoCol(k)
    End Sub

    Function KontoCol(Kontonr)
    Dim Konti As Range
    Set Konti = Range("Konto_Col")
    KontoCol = Application.WorksheetFunction.VLookup(Kontonr, Konti, 2, False)
    End Function

    [/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
    Quote Originally Posted by mdmackillop
    This works for me
    [vba]
    Option Explicit

    Sub Test()
    Dim k
    k = 5
    MsgBox KontoCol(k)
    End Sub

    Function KontoCol(Kontonr)
    Dim Konti As Range
    Set Konti = Range("Konto_Col")
    KontoCol = Application.WorksheetFunction.VLookup(Kontonr, Konti, 2, False)
    End Function
    [/vba]
    Thank you so much. It also works for me. It must be the "Option Explicit" that makes the difference.

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Option Explicit forces you to declare variables, but doesn't change the functionality.
    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'

  5. #5
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Mogens, i understand that your question was related to the thread you posted in but it really is best that you start your own (i have done this for you as these posts have been moved) as your problem may be unique
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

Posting Permissions

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