one14four
one14four

Reputation: 43

"Block if without end if" error VBA

I tried looking through the other answers, but nothing really seemed to help me out. As the title states, I'm getting a "Block if without end if" error. I'm trying to put in a conditional statement that ends the sub if its met. To be more specific, I'm formatting data that can only be formatted one job at a time. I want to automatically end the sub if it determines there are multiple jobs in the spreadsheet. Here's what I've got so far.

Sub SUBNAMEHERE

(Lots of other code)

JobNo = (code that figures out how many jobs there are)
If JobNo > 1 Then
    MsgBox (warning message)
    End Sub
End If

(The rest of the code)

If anyone could help me out, it would be much appreciated.

Upvotes: 4

Views: 19578

Answers (2)

cHao
cHao

Reputation: 86524

Try Exit Sub rather than End Sub.

When you say End Sub, you're telling VB that you're done defining the routine. So if you haven't ended the If before that, it'll be considered incomplete.

Course, even if you did end the If before that, you'd almost certainly wind up getting errors about code outside of a function. (I don't know VBA all that well..but that's how most flavors of VB work.)

Upvotes: 6

polarysekt
polarysekt

Reputation: 572

Sub SUBNAMEHERE

(Lots of other code)

JobNo = (code that figures out how many jobs there are)
If JobNo > 1 Then
    MsgBox (warning message)
    -------> End Sub  <------ 
End If

(The rest of the code)

The End Sub should go on the bottom of the Sub


And as your the commentors pointed out, it's Exit Sub that you're looking for.

Upvotes: 2

Related Questions