Help with simple routine

chobe

New Member
Joined
Oct 6, 2008
Messages
19
I will never figure this stull out! Must be brain dead....Keep getting Else without If line 3

Code:
If Delete_Lst = Empty Then
    End If
ElseIf Delete_Lst = vbNo Then
    Exit Sub
ElseIf Delete_Lst = vbYes Then
    Delete_List
Else
'
End If

Thanks in advance!
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
I will never figure this stull out! Must be brain dead....Keep getting Else without If line 3

Code:
If Delete_Lst = Empty Then
   [B][COLOR="#FF0000"] End If[/COLOR][/B]
ElseIf Delete_Lst = vbNo Then
    Exit Sub
ElseIf Delete_Lst = vbYes Then
    Delete_List
Else
'
End If

Thanks in advance!

The highlighted "End If" is closing off the "If..Then" above it so your "End If" at the end does not have an "If..Then" to pair with. Omit it and the "If..Then" will end automatically as there is no code in that block to execute.
 
Upvote 0
In this case you can use Select Case statement, I consider it easier to use.

Code:
    Select Case Delete_Lst
        Case Empty
            'Action a
        Case vbNo
            Exit Sub
        Case vbYes
            Delete_List
        Case Else
            'Action b
    End Select
 
Upvote 0
The highlighted "End If" is closing off the "If..Then" above it so your "End If" at the end does not have an "If..Then" to pair with. Omit it and the "If..Then" will end automatically as there is no code in that block to execute.

Thanks Rick! Kinda figured when same End If was last test in the loop. Then Excel gave a clear error "End If without Block If".

Question is then, how do you exit a if then loop without exiting the entire sub?
 
Upvote 0
Thanks Rick! Kinda figured when same End If was last test in the loop. Then Excel gave a clear error "End If without Block If".

Question is then, how do you exit a if then loop without exiting the entire sub?
If you structure your If..Then correctly, only the items you want to be tested will be tested and anything else will just fall through to the statement after the End If. For example,
Code:
If X = 25 Then
  MsgBox "X equals 25"
ElseIf X = 30 Then
  MsgBox "X equals 30"
End If
In the above code, X = 25 or X = 30 is the only thing the IF..Then block will react to... for any other value, the If..Then block simply ignores it and execution resumes at the statement after the End If.
 
Upvote 0
If you structure your If..Then correctly, only the items you want to be tested will be tested and anything else will just fall through to the statement after the End If. For example,
Code:
If X = 25 Then
  MsgBox "X equals 25"
ElseIf X = 30 Then
  MsgBox "X equals 30"
End If
In the above code, X = 25 or X = 30 is the only thing the IF..Then block will react to... for any other value, the If..Then block simply ignores it and execution resumes at the statement after the End If.

Couldn't be more simple! Don't know why I couldn't see that. Thanks again!
 
Upvote 0
Couldn't be more simple! Don't know why I couldn't see that. Thanks again!

If you have the opportunity to review the Select Case statement, you can verify that it is just as simple.

Code:
    Select Case x
        Case 25
            MsgBox "x eq 25"
        Case 30
            MsgBox "x eq 30"
    End Select
 
Upvote 0

Forum statistics

Threads
1,223,954
Messages
6,175,603
Members
452,658
Latest member
GStorm

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