Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Block if without end if" error VBA

Tags:

excel

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.

like image 421
one14four Avatar asked Feb 12 '23 09:02

one14four


2 Answers

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.)

like image 167
cHao Avatar answered Feb 20 '23 20:02

cHao


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.

like image 30
polarysekt Avatar answered Feb 20 '23 21:02

polarysekt