Consulting

Results 1 to 18 of 18

Thread: VLOOKUP in VBA

  1. #1
    VBAX Regular
    Joined
    Jul 2012
    Posts
    13
    Location

    VLOOKUP in VBA

    Hello my friends.

    Everyone knows the "VLOOKUP" function in excel right? So what I want to do is the same function but in VBA and with two ranges insted of only one.

    Goal: from an input value from a TextBox, I want to search it in two different sheets knowing that the value will be only in one of them.
    To do that I made the following code that should do something like this: First it looks in Sheet1 that has the maximum of 25 rows. However, if it founds an empty cell then it should interrupt the cycle and go search the value in Sheet2. The same way like in Sheet1, if founds the value than interrupts the cycle but if founds a empty cell then it gives a message that the value wasn't found.

    [VBA] ...
    Worksheets("Sheet1").Activate
    For irowP = 4 To 25
    If Range("B" & [irowP]) = inputESM Then
    GoTo FimSEproj
    End If
    If Range("B" & [irowP]) = Empty Then
    Worksheets("Sheet2").Activate
    For irowH = 4 To nextrowH + 1
    If Range("B" & [irowH]) = inputESM Then
    GoTo FimSEhist
    End If
    If Range("B" & [irowH]) = Empty Then
    MsgBox "ESM " & inputESM & " not found"
    GoTo FimSUB
    End If
    Next irowH
    End If
    Next irowP

    FimSEproj:
    irow = irowP
    ESMSheet = ActiveSheet.Name
    GoTo NewJob

    FimSEhist:
    irow = irowH
    ESMSheet = ActiveSheet.Name
    GoTo NewJob

    NewJob:
    MsgBox "irowP= " & irowP & "irowH= " & irowH & "irow= " & irow & "FolhaESM= " & FolhaESM

    FimSUB:
    MsgBox "END"
    [/VBA]

    Result: Everything works great except when the value is on Sheet1. In this case I get the message of "ESM not found". I don't get it!

    Please help me

  2. #2
    VBAX Mentor
    Joined
    Jul 2012
    Posts
    398
    Location
    what's nextrowH ?

  3. #3
    VBAX Regular
    Joined
    Jul 2012
    Posts
    13
    Location
    nextrowH are defined previously in the code.

    In this case nextrowH is the total number of filled rows of Sheet2 and is defined as:

    [VBA]
    nextrowH = Application.WorksheetFunction.CountA(Range("B:B")) + 1
    [/VBA]

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    [vba]
    Sub tst()
    On Error Resume Next
    c00 = "searchstring"

    c01 = c00 & " has not been found"
    c01 = Sheet1.Name & Sheet1.Cells.Find(c00, , xlValues, 1).Address
    If Err.Number <> 0 Then c01 = Sheet1.Name & Sheet1.Cells.Find(c00, , xlvalues,1).Address

    MsgBox c01
    End Sub
    [/vba]

  5. #5
    VBAX Mentor
    Joined
    Jul 2012
    Posts
    398
    Location
    Quote Originally Posted by helderw
    nextrowH are defined previously in the code.
    attach please your file

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Look at Find in VBA help, it shows you how to do what you need. Just test the first sheet, if not okay, test the second.
    ____________________________________________
    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

  7. #7
    VBAX Regular
    Joined
    Jul 2012
    Posts
    13
    Location
    First of all, thank you to all of you for your support.

    Because it's a little bit difficult for me to explain the problem I have, I've attached the file (a simplified version) where you can run and see all the code.

    To run the macro you should use the "Imprimir ESM" CommandButton on Sheet1.

    The ESM number correspondes to the values on column "B". Sometimes I want values from Sheet1 and other times from Sheet2.

    To help you out, the macro should work properly for the values, for exemple, 12118 , 12116 and 12108. At ths moment it only works with the last one.

    This is one of the reasons that makes me think that if the value is formated as text or as number it will be recognised or not.

    But right now I'm a little bit lost

    Can you help me?
    Attached Files Attached Files

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [VBA]Private Sub cmdPrintOK_Click()

    Dim inputESM As Long
    Dim iRow As Long
    Dim ws As Worksheet

    Application.ScreenUpdating = False

    inputESM = Val(txbInputESM.Text)

    'Determina o numero máximo de linhas da folha "Histórico"
    Set ws = Worksheets("Sheet1")
    iRow = FindESM(ws.Columns(2), inputESM)
    If iRow = 0 Then

    Set ws = Worksheets("Por sair...")
    iRow = FindESM(ws.Columns(2), inputESM)
    If iRow = 0 Then

    Set ws = Worksheets("Sheet2")
    iRow = FindESM(ws.Columns(2), inputESM)
    End If
    End If

    If iRow > 0 Then

    MsgBox inputESM & " found on " & ws.Name & " in row " & iRow, vbInformation + vbOKOnly, "Find ESM"
    Else

    MsgBox inputESM & " not found", vbCritical + vbOKOnly, "Find ESM"
    End If

    Unload printESMform

    End Sub

    Private Function FindESM(LookIn As Range, ESM As Long) As Long
    Dim cell As Range

    Set cell = LookIn.Find(ESM, LookIn.Cells(1, 1))
    If Not cell Is Nothing Then FindESM = cell.Row
    End Function
    [/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

  9. #9
    VBAX Regular
    Joined
    Jul 2012
    Posts
    13
    Location
    xld, my friend, thank you a lot for your code.

    I'm amazed how you did the same thing in a tottaly different way. I have a lot to learn.

    However I didn't quite understand how the function "FindESM" works. Could you please explain it to me?

    Once again thank's a lot.

  10. #10
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    That is how I was suggesting doing it in my previous response.

    It just uses the VBA Find function to look in the specified column for the specified value. If found, it returns the row number found in, otherwise it returns 0; the calling procedure determines what to do with that result.
    ____________________________________________
    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

  11. #11
    VBAX Regular
    Joined
    Jul 2012
    Posts
    13
    Location
    It's much easier this way. Thank you one more time.

    Meanwhile I have another problem... Maybe you could help me.

    I want to make a print preview of "Capa" woorksheet but for some reason it doesn't work.

    I've introduced the following code inside the If cycle you made:

    [VBA]
    If iRow > 0 Then

    MsgBox inputESM & " found on " & ws.Name & " in row " & iRow, vbInformation + vbOKOnly, "Find ESM"

    Worksheets("Capa").Range("U1") = ws.Range("B" & [iRow]).Value
    'Prints "Capa"
    If cbxPrePrint.Value = True Then
    Sheets("Capa").PrintOut Preview:=True
    'Else
    'Sheets("Capa").PrintOut
    End If

    'Deletes "Capa" info
    Worksheets("Capa").Range("U1,U2,U3,N5,N6,T7,P30,P31,P32,M8,N8,O8,P8") = ""

    Else

    MsgBox inputESM & " not found", vbCritical + vbOKOnly, "Find ESM"
    End If

    Unload printESMform
    [/VBA]

    When I select in the ESMform the checkbox "Pré-visualizar" (=preview) I should be able to preview the print area.

    I think the problem may be because this instruction is inside the If loop.

    Do you have any sugestion?

  12. #12
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    It is probably because the userform is still loaded. Try moving the unload to before the printpreview If statement.

    If that doesn't work, post the workbook and we can take a look.
    ____________________________________________
    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

  13. #13
    VBAX Regular
    Joined
    Jul 2012
    Posts
    13
    Location
    I had already tried to move the Unload but without sucess.

    It seems it is in a "infinite loop".

    Please could you take a look? Thanks a lot.
    Attached Files Attached Files

  14. #14
    VBAX Regular
    Joined
    Jul 2012
    Posts
    13
    Location
    Does anyone have any sugestion regarding the print preview issue?

    Thanks!

  15. #15
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    as per xld's suggestion:

    [VBA]
    If cbxPrePrint.Value = True Then
    Unload Me
    Sheets("Capa").PrintOut Preview:=True
    Else
    Sheets("Capa").PrintOut
    End If
    [/VBA]
    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)

  16. #16
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    [VBA]
    sub snb()
    Hide
    Sheets("Capa").PrintOut cbxPrePrint.Value
    end sub
    [/VBA]

  17. #17
    VBAX Regular
    Joined
    Jul 2012
    Posts
    13
    Location
    Quote Originally Posted by mancubus
    as per xld's suggestion:

    [vba]
    If cbxPrePrint.Value = True Then
    Unload Me
    Sheets("Capa").PrintOut Preview:=True
    Else
    Sheets("Capa").PrintOut
    End If
    [/vba]
    I have already tried that but it doesn't work. I mean, the "print Preview" menu doesn't show up. You can see this if you run the file I attached before.

    Thanks anyway.

  18. #18
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Try this
    Attached Files Attached Files
    ____________________________________________
    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

Posting Permissions

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