Why won't this run with IF statement & MsgBox where it is

Brian Feth

New Member
Joined
May 21, 2017
Messages
37
Window 11, Excel 2021 Why wont this run with the If statement (between the question marks) where it is. It runs fine without the If Statement. I get the message "Compile Error: Expected End Sub" while highlighting the "H" in the line, " If Range("H" & FirstVender2Row).Value = "Missing Vender" Then ". Help is always appreciated!

For x = 0 To NumVender1ToFill
ChosenDescript = Range("J" & FirstVender1Row).Value

Do Until FirstVender2Row > LastVender2Row
Vender = Range("H" & FirstVender2Row).Value

'????????????????????????????????????????????????????????????????????
If Range("H" & FirstVender2Row).Value = "Missing Vender" Then
Sub MsgBoxAbortRetryIgnore()
MsgBox "Missing Vender, Press Abort, Hand Fill in Vender", vbAbortRetryIgnore
End Sub
End If
'???????????????????????????????????????????????????????????????????

If InStr(ChosenDescript, Vender) <> 0 Then 'Match, Positive result, True
Range("H" & FirstVender1Row).Value = Vender 'Fills Vender name adjacent to Description
FirstVender2Row = Range("H595").Row 'Resets the Vender row
FirstVender1Row = FirstVender1Row - 1
Exit Do

ElseIf InStr(ChosenDescript, Vender) = 0 Then 'No match, negitive result, False
FirstVender2Row = FirstVender2Row + 1
End If
Loop
Next x
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
the variable "FirstVender2Row" doesn't appear to have a starting value ??
 
Upvote 0
It runs fine without the If Statement. I get the message "Compile Error: Expected End Sub"

That's not running fine. That's the compiler tell you there is a problem.

Sub and End Sub define the beginning and end of a subroutine, you can't use it as you are doing, inside another subroutine. Why not just comment them out?

VBA Code:
    '????????????????????????????????????????????????????????????????????
    If Range("H" & FirstVender2Row).Value = "Missing Vender" Then
        'Sub MsgBoxAbortRetryIgnore()
        MsgBox "Missing Vender, Press Abort, Hand Fill in Vender", vbAbortRetryIgnore
        'End Sub
    End If
    '???????????????????????????????????????????????????????????????????
 
Upvote 0
You can't just place a macro in another macro like that

Try this:
VBA Code:
If Range("H" & FirstVender2Row).Value = "Missing Vender" Then
    MsgBox "Missing Vender, Press Abort, Hand Fill in Vender", vbAbortRetryIgnore
    Exit Sub
End If

although using vbAbortRetryIgnore without testing for any of those conditions isn't doing anything. You should use vbOKOnly to use the code as written.

BTW, Vender should be spelled Vendor
 
Upvote 0
Solution
That's not running fine. That's the compiler tell you there is a problem.

Sub and End Sub define the beginning and end of a subroutine, you can't use it as you are doing, inside another subroutine. Why not just comment them out?

VBA Code:
    '????????????????????????????????????????????????????????????????????
    If Range("H" & FirstVender2Row).Value = "Missing Vender" Then
        'Sub MsgBoxAbortRetryIgnore()
        MsgBox "Missing Vender, Press Abort, Hand Fill in Vender", vbAbortRetryIgnore
        'End Sub
    End If
    '???????????????????????????????????????????????????????????????????

Window 11, Excel 2021 Why wont this run with the If statement (between the question marks) where it is. It runs fine without the If Statement.
That's not running fine. That's the compiler tell you there is a problem.

Sub and End Sub define the beginning and end of a subroutine, you can't use it as you are doing, inside another subroutine. Why not just comment them out?

VBA Code:
    '????????????????????????????????????????????????????????????????????
    If Range("H" & FirstVender2Row).Value = "Missing Vender" Then
        'Sub MsgBoxAbortRetryIgnore()
        MsgBox "Missing Vender, Press Abort, Hand Fill in Vender", vbAbortRetryIgnore
        'End Sub
    End If
    '???????????????????????????????????????????????????????????????????

I get the message "Compile Error: Expected End Sub" while highlighting the "H" in the line, " If Range("H" & FirstVender2Row).Value = "Missing Vender" Then ". Help is always appreciated!

For x = 0 To NumVender1ToFill
ChosenDescript = Range("J" & FirstVender1Row).Value

Do Until FirstVender2Row > LastVender2Row
Vender = Range("H" & FirstVender2Row).Value

'????????????????????????????????????????????????????????????????????
If Range("H" & FirstVender2Row).Value = "Missing Vender" Then
Sub MsgBoxAbortRetryIgnore()
MsgBox "Missing Vender, Press Abort, Hand Fill in Vender", vbAbortRetryIgnore
End Sub
End If
'???????????????????????????????????????????????????????????????????

If InStr(ChosenDescript, Vender) <> 0 Then 'Match, Positive result, True
Range("H" & FirstVender1Row).Value = Vender 'Fills Vender name adjacent to Description
FirstVender2Row = Range("H595").Row 'Resets the Vender row
FirstVender1Row = FirstVender1Row - 1
Exit Do

ElseIf InStr(ChosenDescript, Vender) = 0 Then 'No match, negitive result, False
FirstVender2Row = FirstVender2Row + 1
End If
Loop
Next x
I wrote “Why won’t this run WITH the If statement. It runs fine WITHOUT the If Statement”. Followed by “I get the message "Compile Error: Expected End Sub…….”. I realize the computer is telling me there is a problem. That’s why I submitted a question.
 
Upvote 0
You can't just place a macro in another macro like that

Try this:
VBA Code:
If Range("H" & FirstVender2Row).Value = "Missing Vender" Then
    MsgBox "Missing Vender, Press Abort, Hand Fill in Vender", vbAbortRetryIgnore
    Exit Sub
End If

although using vbAbortRetryIgnore without testing for any of those conditions isn't doing anything. You should use vbOKOnly to use the code as written.

BTW, Vender should be spelled Vendor
That works as intended. Thank you so much! At some point I realize I had written both "Vender" and "Vendor" in different places in the program so I looked it up. My Dictionary said "Vendor" is the English spelling and "Vender" is the American spelling so I picked one. Thank you again.
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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