If Else if not working

youcanreachtrevor

New Member
Joined
May 17, 2018
Messages
5
I had an if condition written and it worked just fine, but I want to add to it a condition that will allow the code to stop a couple lines before the end of the code, but the else if won't work. I keep getting an error saying "Compile error: expected: Do or For or Sub or Function or Property"
I'm just getting back into writting VBA, so I'm a bit rusty, but I couldn't find this in any other threads. I'm sure the question is a simple one. Not sure what I'm missing. ANy insight would be helpful thanks.

Code:
Public Sub NextItem()
Gap = 1
For p = ActiveRow To LastRow - 1
If Len(WorksheetFunction.Trim(WorksheetFunction.Clean(NISSCADSH.Cells(ActiveRow + Gap, 4).Value))) <> 0 Then
        FutureRow = ActiveRow + Gap
        Exit Sub
    
    Else if (p = (LastRow - 2)) then exit

    Else: Gap = Gap + 1
    End If
Next
End Sub
 
Last edited by a moderator:

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Hi & welcome to the board.
How about
Code:
Public Sub NextItem()
Gap = 1
For p = ActiveRow To LastRow - 1
   If Len(WorksheetFunction.Trim(WorksheetFunction.Clean(NISSCADSH.Cells(ActiveRow + Gap, 4).Value))) <> 0 Then
      FutureRow = ActiveRow + Gap
      Exit Sub
   ElseIf (p = (LastRow - 2)) Then
      Exit Sub
   Else: Gap = Gap + 1
   End If
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,320
Members
452,635
Latest member
laura12345

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