VBA: What's the difference b/n End and Exit Sub

xenou

MrExcel MVP
Joined
Mar 2, 2007
Messages
16,836
Office Version
  1. 2019
Platform
  1. Windows
When would I choose one of these over the other? What difference does it make?

Thanks all.
 
End Sub is the last statement in a sub.
Exit Sub must be used in all other cases.

You have no options in this matter.
 
Upvote 0
Hi,

END stops really all code
Exit Sub does what is says: going out of the currently running SUB

END is drastic !!

example: run MAINsub
if you press YES in first macro, you will not see the next messagebox, but you will goto macro2
if you press YES in second macro you will never see the third

Code:
Sub main()
macro1
macro2
macro3
End Sub

Sub macro1()
If MsgBox("exit sub?", 36, "MACRO1") = vbYes Then Exit Sub
MsgBox "you didn't press YES", 64, "MACRO1"
End Sub
Sub macro2()
If MsgBox("end?", 36, "MACRO2") = vbYes Then End
MsgBox "you didn't press YES", 64, "MACRO2"
End Sub
Sub macro3()
MsgBox "Third macro", 64, "MACRO3"
End Sub
kind regards,
Erik
 
Upvote 0
This is an old thread, however it pertains to a problem I'm having.
Last line is End Sub - error expected End With.
I changed it to End With - error expected End Sub.
I'm really missing something . .. .

Code:
Sub border_highlight_macro()

With ActiveSheet
    .Range(.Cells(9, 4), .Cells(50, 4)).Borders.LineStyle = Excel.XlLineStyle.xlLineStyleNone
 
   .Range("D9:D50").Select
    With Selection.Interior
        .Pattern = xlNone
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    Range("E9:E50").Select
    Range("E50").Activate
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 13434828
        .TintAndShade = 0
        .PatternTintAndShade = 0
   End With

Appreciate any help with this,
Mike
 
Upvote 0
This is an old thread, however it pertains to a problem I'm having.
Last line is End Sub - error expected End With.
I changed it to End With - error expected End Sub.
I'm really missing something . .. .

Code:
Sub border_highlight_macro()

With ActiveSheet
    .Range(.Cells(9, 4), .Cells(50, 4)).Borders.LineStyle = Excel.XlLineStyle.xlLineStyleNone
 
   .Range("D9:D50").Select
    With Selection.Interior
        .Pattern = xlNone
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    Range("E9:E50").Select
    Range("E50").Activate
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 13434828
        .TintAndShade = 0
        .PatternTintAndShade = 0
   End With

Appreciate any help with this,
Mike

Dear Mike
U started with With Activesheet The End with for that is missing I suppose ,Pl chk

Regards
Venkat
 
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