Consulting

Results 1 to 4 of 4

Thread: VLOOKUP

  1. #1
    VBAX Regular
    Joined
    Nov 2022
    Posts
    13
    Location

    Question VLOOKUP

    Hi everyone,
    I want to use de function "vlookup" on vba, but when the macro cannot search the value X on page ("CNM_ABER") i pretend that the variable y will be EMPTY. What you recommended that i do?
    In this moment i have this code:


    For i = 1 To 169
    Worksheets("CNM_LIB").Select
    x = Cells(1 + i, "F").Value
    Worksheets("CNM_ABER").Select
    On Error Resume Next
    y = WorksheetFunction.VLookup(x, Range("F1:N221"), 8, False)
    'y = WorksheetFunction.IfError(WorksheetFunction.VLookup(x, Range("F1:N221"), 8, False), "")
    On Error GoTo 0
    End Sub
    Last edited by Aussiebear; 02-22-2023 at 12:33 PM. Reason: Added code tags to supplied code

  2. #2
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    335
    Location
    Why is there not a Next line to accompany the For? On Error Resume Next has nothing to do with controlling a loop iteration.

    Do not need to Select worksheets or other objects in order to act on them.

    Cells property requires numeric arguments

    x = Worksheets("CNM_LIB").Cells(1 + i, 6).Value
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,875
    Quote Originally Posted by June7 View Post
    Cells property requires numeric arguments
    Cells(22,"F") is good.

    Guessing:
    For i = 1 To 169
      y = Application.VLookup(Worksheets("CNM_LIB").Cells(1 + i, "F").Value, Worksheets("CNM_ABER").Range("F1:N221"), 8, False)
      If IsError(y) Then    'it's not found
        'do something (or nothing?)
      Else    'it has been found (and y contains the result)
        'do something else
      End If
    Next i
    Last edited by p45cal; 02-22-2023 at 03:20 PM.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  4. #4
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    335
    Location
    Well, dagnabbit! Would be nice if MS docs mentioned that.

    Could have sworn I tried it at some time long ago.
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Tags for this Thread

Posting Permissions

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