Results 1 to 14 of 14

Thread: VLookup VBA Code & Elseif troubles

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #6
    VBAX Mentor
    Joined
    Dec 2008
    Posts
    404
    Location
    p45cal, I would write the second code a little differently. First, I would check if X or Y (or both) contain an error. Then, Select Case performed only when there is no error.
    Do While Cells(i, 1).Value <> ""
        With Application
          x = .VLookup(sht1.Cells(i, 1), sht2.Range("A:H"), 7, 0)
          y = .VLookup(sht1.Cells(i, 1), sht2.Range("A:H"), 8, 0)
        End With
          
          If Not IsError(x) Then
            Select Case True
              Case x = 2 And y = 1: Cells(i, 5) = "T+1"
              Case x = 0 And y = 0: Cells(i, 5) = "T+0"
              Case x = 1 And y = 1: Cells(i, 5) = "T-1"
              Case x = 1 And y = 2: Cells(i, 5) = "T-2"
              Case x = 1 And y = 3: Cells(i, 5) = "T-3"
            End Select
        End If
        
        i = i + 1
    Loop
    noob93, You must know that there is a difference in error handling (when the sheet function returns an error), depending on the context in which you call this function.
    If it do not find the result:
    - Application.WorksheetFunction.VLookup creates a runtime error,
    - Application.VLookup returns a sheet error (e.g., #N/A!)

    I think Charles Pearson will explain it enough http://www.cpearson.com/Excel/Callin...ionsInVBA.aspx

    Artik
    Last edited by Artik; 07-10-2019 at 06:26 AM.

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
  •