Consulting

Results 1 to 10 of 10

Thread: Vlookup a text and MsgBox it thanks to VBA

  1. #1

    Vlookup a text and MsgBox it thanks to VBA

    Hello everybody,

    I'trying to Vlookup a value thanks to an input box and after, msg box the result, how can I do that thanks to VBA ?

    I already wrote this code but it's not working.

    Sub Vlookup()
    ' Vlookup Macro
    Dim LookUpValue As String
    Dim Lookup As Variant
    LookUpValue = InputBox("Enter the name")
        
    Lookup = Application.WorksheetFunction.Vlookup(LookUpValue, Sheet2.Range("B2:C5"), 2, False)
    MsgBox Lookup
    'MsgBox "The number is " & Lookup
    End Sub
    Here is my table:

    Thanks by advance
    A B C
    1 ROM 1
    2 TOM 2
    3 DAN 3
    4 NIC 4

  2. #2
    VBAX Expert mperrah's Avatar
    Joined
    Mar 2005
    Posts
    744
    Location
    Try this,
    I tested this with your data.
    It asks the user for the name to find,
    then messages the number it finds.

    Sub matchOffset()
    Dim x As Long
    Dim lr, lookRng As Range
    Dim findStr As String
    Dim foundCell As Variant
    
    
    lr = Cells(Rows.Count, "B").End(xlUp).Row
    Set lookRng = Range("B1:B" & lr)
    
    findStr = InputBox("Enter the name to find")
    
        For x = 1 To lr
            If Range("B" & x).Value = findStr Then
            Set foundCell = Range("B" & x).Offset(0, 1)
            MsgBox foundCell & " was found to Match"
            End If
        Next x
    
    End Sub
    -mark

  3. #3
    Ok, the code works perfectly and I understood it ^^,

    But could I have done it with Application.Vlookup ?

    Thank you

  4. #4
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    @nhiribarne

    explaining your requirement in full detail will help helpers understand your case better than insisting on some methods.

    if you explain your ultimate purpose, helpers may provide different solutions, which will help you improve your vba skills.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  5. #5
    Ok, I'll try to be more accurate on my explanation.
    I would like to learn how to use the excel function on VBA. I found this website and try to work with it (cf my first post and the macro I tried to wrote)
    http://www.exceltrick.com/formulas_m...lookup-in-vba/

    But I could not get it, telling me that there is a "bug" in the

    Lookup = Application.WorksheetFunction.Vlookup(LookUpValue, Sheet2.Range("B2:C5"), 2, False)
    part.

    If I'm not accurate enough, please, let me know :-)

    Thank you for your help again

  6. #6
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    when i copied the your table in a blank sheet and run the code you pasted, it worked for me.

    check the range references. is your table in Sheet2 and in B2:C5 range...
    check if the cells in data table have non printing characters...
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  7. #7
    Ok, it works for me too ...
    I don't know what was the problem with my workbook yesterday, thank you very much both of you guys anyway :-)

    Subject solved.

  8. #8
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    you are welcome.

    pls mark the thread as solved from the Thread Tools dropdown which is above the first message.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  9. #9
    VBAX Expert mperrah's Avatar
    Joined
    Mar 2005
    Posts
    744
    Location
    Check this out Using drop down Val instead of input box (less typos)

    I used a dynamic named range for column B
    name is LookUpB
    =OFFSET(Sheet2!$B$1,0,0,COUNTA(Sheet2!$B:$B))
    then added a list data validation drop down in G1 (could go anywhere)
    source =LookUpB (as we just named the range of names)
    vlookup.jpg

    Drop this on the Sheet2 code. it will run the lookup when ever you change the drop down value
    Private Sub Worksheet_Change(ByVal Target As Range)
    
        If Target.Column = 7 _ ' is G1, just update if you move the drop down
            And Target.Row = 1 Then
            Call Vlookup
        End If
    
    End Sub
    then alter the Vlookup macro to this
    Sub Vlookup()
    ' Vlookup Macro
    Dim LookUpValue As String
    Dim Lookup As Variant
    Dim LookUpWhat As String
    
    LookUpWhat = Sheets(2).Range("G1") ' make this where ever you want the drop down to pick the name to look up
    
    Lookup = Application.WorksheetFunction.Vlookup(LookUpWhat, Sheet2.Range("B2:C5"), 2, False)
    MsgBox Lookup
    'MsgBox "The number is " & Lookup
    End Sub
    The cool thing with this is as you add names and numbers on sheet2, the code won't need to be changed

  10. #10
    Wow love it, thx a lot ;-)

Posting Permissions

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