ElseIf Loop

GonzoB

New Member
Joined
Dec 4, 2021
Messages
43
Hi All,

my vba below works well but I need to enhance it. I want that if Sheets("Output).Cells(i, 1) is blank, then cell Sheets("Output).Cells(i, 7) becomes blank too. I was trying a lot to add ElseIf to the actual code but I could not make it work. Can you help how to add ElseIf appropriately? Thanks.

VBA Code:
For j = 2 To OutputRow

Dim lookupVal_2 As Range
Dim myString_2 As Variant, Rng1$
On Error Resume Next
    Set lookupVal_2 = Sheets("Output").Cells(j, 5)
    Rng1 = Range(Cells(2, 3), Cells(InputRow, 3)).Address
    myString_2 = WorksheetFunction.VLookup(lookupVal_2, Sheets("Input").Range(Rng1), 1, False)
    If Err.Number > 0 Then
        Sheets("Output").Cells(j, 7) = "action 1"
        Err.Clear
    Else
        Sheets("Output").Cells(j, 7) = "action 2"
    
    End If
    
    
    Next j
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Replace the part of your code that you put with the following code:

VBA Code:
  Dim lookupVal_2 As Range
  Dim myString_2 As Variant, Rng1$

  For j = 2 To OutputRow
    If Sheets("Output").Cells(j, 1).Value = "" Then
      Sheets("Output").Cells(j, 7).Value = ""
    Else
      On Error Resume Next
      Set lookupVal_2 = Sheets("Output").Cells(j, 5)
      Rng1 = Range(Cells(2, 3), Cells(InputRow, 3)).Address
      myString_2 = WorksheetFunction.VLookup(lookupVal_2, Sheets("Input").Range(Rng1), 1, False)
      If Err.Number > 0 Then
          Sheets("Output").Cells(j, 7) = "action 1"
          Err.Clear
      Else
          Sheets("Output").Cells(j, 7) = "action 2"
      End If
    End If
  Next j
 
Upvote 0
Solution

Forum statistics

Threads
1,215,094
Messages
6,123,071
Members
449,092
Latest member
ipruravindra

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top