Consulting

Results 1 to 6 of 6

Thread: Using VBA to LOOKUP

  1. #1
    VBAX Mentor
    Joined
    Nov 2008
    Posts
    305
    Location

    Using VBA to LOOKUP

    I have a variable which contains a string (to letters, for example DE)
    I need some code that will take the variable, and look at an array (Worksheet = "Sheet2" Range ="A1:A249") If it finds the letters then do something, if not then do something else.

    Whats the best way to solve this? I'm guessing it is part of an If statement, but I'm not sure how to try to match/find the variable.
    Also, I don't know if this will be relevant, but it will be part of a loop, as the variable will need to constantly change.

    Thanks for your help.

  2. #2
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    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.
    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)

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Dim myVar As String
    Dim cell As Range

    myVar = "DE"
    Set cell = Worksheets("Sheet2").Range("A1:A249").Find(myVar)
    If Not cell Is Nothing Then

    MsgBox "do something"
    End If
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    VBAX Mentor
    Joined
    Nov 2008
    Posts
    305
    Location
    Thanks both for your replies.
    I went for the Lookup solution.
    However, I've been able to apply the Find solution to another workbook.
    Question using the code below, how can I get the cell reference if the find is successfull?

    [vba]
    Dim myVar As String
    Dim cell As Range
    Dim mycellref as string

    myVar = "DE"
    Set cell = Worksheets("Sheet2").Range("A1:A249").Find(myVar)
    If Not cell Is Nothing Then
    mycellref = cell.address 'provide my cell reference
    MsgBox "do something"
    End If [/vba]


    Never mind, got it

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Aren't you already doing just that?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    VBAX Mentor
    Joined
    Nov 2008
    Posts
    305
    Location
    I am now thanks, I edited the code, when I wrote that I had found the solution, to show the solution.

    Thanks for your help. :-)

Posting Permissions

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