End if without block if - What am I doing wrong?

Stanski21

New Member
Joined
Apr 13, 2016
Messages
7
I'm either trying to make this too simple or not complex enough... I'm trying to add in a simple qualifier as to whether a macro will run. It heads up the code with nothing in front of it and the macro would run completely fine without it. Here's the code:

Range("K1").Formula = "=countif(E2:E500, 0)"


If Range("K1") = 0 Then MsgBox "No agents to remove", vbOKOnly + vbInformation
Exit Sub
End If

The code continues on to run the filter, sort and remove macro I've written. But I get the error "End If without block IF" highlighting the above.
If I remove the End IF and the countif results >0, then the macro still doesn't run.

What am I doing wrong?
 
You need:

Code:
If Range("K1") = 0 Then 
MsgBox "No agents to remove", vbOKOnly + vbInformation
Exit Sub
End If
 
Upvote 0
Thanks! So it was a simple answer... I need to read up more about the importance of using lines to seperate elements of code... :eeek:
 
Upvote 0
FYI here are two alternative forms no end if required.

If you only wanted to display a message box, no exit sub, then you could use this:
Code:
    If Range("K1") = 0 Then MsgBox "No agents to remove", vbOKOnly + vbInformation
End Sub

With the exit sub statement you can use this code:
Code:
    If Range("K1") = 0 Then MsgBox "No agents to remove", vbOKOnly + vbInformation: Exit Sub
End Sub
 
Last edited:
Upvote 0

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