Results 1 to 14 of 14

Thread: VLookup VBA Code & Elseif troubles

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,970
    the OnError Resume Next line is your problem.
    I've made your code a little easier to read and made a minor adjustment. Can you test it for me to see if it still behaves in the same way. If an error pops up, tell me what the error says and what line it occurs on.
    Dim j As Integer
    Dim book1 As Workbook
    Dim book2 As Workbook
    Dim sht1 As Worksheet, sht2 As Worksheet, x, y
    
    Set book1 = Workbooks("Proves Dades Operatives")
    Set book2 = Workbooks("R71 1i2")
    Set sht1 = book1.Sheets("Hoja1")
    Set sht2 = book2.Sheets("Hoja3")
    i = 2
    
    Do While Cells(i, 1).Value <> ""
      x = Application.VLookup(sht1.Cells(i, 1), sht2.Range("A:H"), 7, 0)
      y = Application.VLookup(sht1.Cells(i, 1), sht2.Range("A:H"), 8, 0)
    
      If x = 2 And y = 1 Then
        Cells(i, 5) = "T+1"
      ElseIf x = 0 And y = 0 Then
        Cells(i, 5) = "T+0"
      ElseIf x = 1 And y = 1 Then
        Cells(i, 5) = "T-1"
      ElseIf x = 1 And y = 2 Then
        Cells(i, 5) = "T-2"
      ElseIf x = 1 And y = 3 Then
        Cells(i, 5) = "T-3"
      End If
      i = i + 1
     
     ' On Error Resume Next
    Loop
    You can also test the following.
    Dim i As Integer
    Dim j As Integer
    Dim book1 As Workbook
    Dim book2 As Workbook
    Dim sht1 As Worksheet, sht2 As Worksheet, x, y
    
    Set book1 = Workbooks("Proves Dades Operatives")
    Set book2 = Workbooks("R71 1i2")
    Set sht1 = book1.Sheets("Hoja1")
    Set sht2 = book2.Sheets("Hoja3")
    i = 2
    
    Do While Cells(i, 1).Value <> ""
      x = Application.VLookup(sht1.Cells(i, 1), sht2.Range("A:H"), 7, 0)
      y = Application.VLookup(sht1.Cells(i, 1), sht2.Range("A:H"), 8, 0)
    
      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
      i = i + 1
    Loop
    This code should NOT require the On Error Resume Next line at all.
    Last edited by p45cal; 07-09-2019 at 02:28 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.

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
  •