Exit All Subs when condition in first sub loop is met

SGTSparty

New Member
Joined
Oct 30, 2015
Messages
12
Hi everyone, I'm a VBA/Macro newb and with the help of some folks on here and some friends I've built the following 4 Macros:

Code:
Sub BlankFinder() 'Prevents blank names in PowerPackNames. Blanks cause follow-on Macros to fail and cannot be completely accounted for in Data Validation

For c = 3 To 17 'sets C as variable below for Column # for Col C (3) through Q (17)


If Cells(3, c) = "" Then 'if Cells C3-Q3 (determined by C variable) are blank then MsgBox.
    MsgBox "Power Pack Names Cannot Be Blank"
    ElseIf IsEmpty(Cells(3, c)) Then 'If cells C3-Q3 (determined by C variable) are empty then MsgBox. Not sure diff between Blank and Empty. Covering Bases.
    MsgBox "Power Pack Names Cannot Be Blank"
    Else: End If 'If cell is not blank nor empty then do nothing.
Next c 'for each "C" (the cell in each column determined by the C variable) this loop is run. If the cell is not blank or empty it moves on to the next C.


End Sub
Sub Rename() 'This Macro renames each worksheet with the name in cell A1 of that sheet.
'A1 is formula that throughputs the Power Pack Name from Step1 C3-Q3. Sheet name restirctions are controlled in those cells.
'string length, special characters and duplicates are controlled using Data Validation forumlas.
'Blanks are controlled with the BlankFinder Macro above.
     
    Dim ws As Worksheet 'sets ws as shorthand for worksheet.
     
    For Each ws In Sheets 'Tells the Macro to run for ALL worksheets.
        ws.Name = ws.Range("A1") 'Since this runs for ALL worksheets, this means that Step1 and Admin also have their titles in A1. in Step1 hidden by Button.
    Next ws 'Once a sheet is renamed to whatever is in A1 moves on to next Worksheet.
     
End Sub
Sub Hide() 'This macro hides worksheets that are not currently in use by the user.


For c = 3 To 5 'This sets the Variable C. C is the column number in the cell address used below. C provides the range of cells the macro will work for.
'In practicle terms this sets what sheets are being effected by this macro based on matching the sheet names to the names in C3-Q3.
'These sheets are to be hidden if the dropdown in C2-Q2 for that sheets corresponding column is set to "No" and made visible if set to "Yes"
'Ensure that the To number above is set to you column number of the last cell in Power Pack Names that has a matching Worksheet set.
'This also means that the worksheet naming convention must match that stated below. This step is handled by the Rename Macro above.


If Cells(2, c) = "No" Then 'If drop down in Row 2 for the corresponding column C is set to No
Worksheets(CStr(Cells(3, c) & "Step2")).Visible = False 'Then hide worksheet that matches the naming convention in that column row 3 concatinated with "Step2"
Worksheets(CStr(Cells(3, c) & "PartsCatalog")).Visible = False 'Then hide worksheet that matches the naming convention in that column row 3 concatinated with "PartsCatalog"
'This also means that if the worksheet naming conventions change from Step2 and PartsCatalog the macro needs to change to match.
Else
Worksheets(CStr(Cells(3, c) & "Step2")).Visible = True 'Then unhide/leave visible worksheet that matches the naming convention in that column row 3 concatinated with "Step2"


Worksheets(CStr(Cells(3, c) & "PartsCatalog")).Visible = True 'Then unhide/leave visible worksheet that matches the naming convention in that column row 3 concatinated with "PartsCatalog"
End If


Next c 'Once this is done move on to next cell as determined by C variable


End Sub
Sub RunAllMacros() 'This Macro is used to run all the above macros, in order.
'This is done to try and prevent any error states in the Hide Macro by making sure that any naming issues hare fixed by the naming macro first
'This also prevens the Renaming macro from hitting an error state from trying to rename worksheets based on blank titles, which would cause duplicate worksheet names
'This is prevented by running the BlankFinder immediately before hand.
'The blank finder combined with Data Validation rules w/in the cells should prevent the vast majority of error states.
BlankFinder
Rename
Hide
End Sub

They work perfectly with one exception: When a the BlankFinder finds a Blank cell, the MsgBox pops up but continues through the loops and when using RunAllMacros not only does it continue through the loops but Rename and Hide also run. This typically isn't a problem but if more than one C is left blank then you get a VBA 400 error. I'm trying to user proof this because people in my office are leery of Macros/VBA so broken macro/vba pop ups they don't understand will cause them to quit using it entirely.

What I'd like to happen is that when BlankFinder finds a blank cell it gives an MsgBox and stops running. In RunAllMacros if Blankfinder finds a blank I want it to end all process, so it doesn't continue through the loops and doesn't run Rename or Hide. I'm sure this is possible I just can't figure out how with my limited knowledge.

Any advice would be very much appreciated.

Quick note, as I said I had help with these from here and elsewhere so I don't have answers as to why i did it this way vs. that other than it worked. I'm not married to any of these solutions. Also in the Hide Sub it says 3 to 5 instead of 3 to 17 because I'm still working on the mechanics of the PartsCatalog and so I've only created sheets for 1, 2 and 3 so I can test these out. There will eventually be 15 pairs of sheets but currently there are only 3. Thanks again
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Hi everyone, I'm a VBA/Macro newb and with the help of some folks on here and some friends I've built the following 4 Macros:

Code:
Sub BlankFinder() 'Prevents blank names in PowerPackNames. Blanks cause follow-on Macros to fail and cannot be completely accounted for in Data Validation

For c = 3 To 17 'sets C as variable below for Column # for Col C (3) through Q (17)


If Cells(3, c) = "" Then 'if Cells C3-Q3 (determined by C variable) are blank then MsgBox.
    MsgBox "Power Pack Names Cannot Be Blank"
    ElseIf IsEmpty(Cells(3, c)) Then 'If cells C3-Q3 (determined by C variable) are empty then MsgBox. Not sure diff between Blank and Empty. Covering Bases.
    MsgBox "Power Pack Names Cannot Be Blank"
    Else: End If 'If cell is not blank nor empty then do nothing.
Next c 'for each "C" (the cell in each column determined by the C variable) this loop is run. If the cell is not blank or empty it moves on to the next C.


End Sub
Sub Rename() 'This Macro renames each worksheet with the name in cell A1 of that sheet.
'A1 is formula that throughputs the Power Pack Name from Step1 C3-Q3. Sheet name restirctions are controlled in those cells.
'string length, special characters and duplicates are controlled using Data Validation forumlas.
'Blanks are controlled with the BlankFinder Macro above.
     
    Dim ws As Worksheet 'sets ws as shorthand for worksheet.
     
    For Each ws In Sheets 'Tells the Macro to run for ALL worksheets.
        ws.Name = ws.Range("A1") 'Since this runs for ALL worksheets, this means that Step1 and Admin also have their titles in A1. in Step1 hidden by Button.
    Next ws 'Once a sheet is renamed to whatever is in A1 moves on to next Worksheet.
     
End Sub
Sub Hide() 'This macro hides worksheets that are not currently in use by the user.


For c = 3 To 5 'This sets the Variable C. C is the column number in the cell address used below. C provides the range of cells the macro will work for.
'In practicle terms this sets what sheets are being effected by this macro based on matching the sheet names to the names in C3-Q3.
'These sheets are to be hidden if the dropdown in C2-Q2 for that sheets corresponding column is set to "No" and made visible if set to "Yes"
'Ensure that the To number above is set to you column number of the last cell in Power Pack Names that has a matching Worksheet set.
'This also means that the worksheet naming convention must match that stated below. This step is handled by the Rename Macro above.


If Cells(2, c) = "No" Then 'If drop down in Row 2 for the corresponding column C is set to No
Worksheets(CStr(Cells(3, c) & "Step2")).Visible = False 'Then hide worksheet that matches the naming convention in that column row 3 concatinated with "Step2"
Worksheets(CStr(Cells(3, c) & "PartsCatalog")).Visible = False 'Then hide worksheet that matches the naming convention in that column row 3 concatinated with "PartsCatalog"
'This also means that if the worksheet naming conventions change from Step2 and PartsCatalog the macro needs to change to match.
Else
Worksheets(CStr(Cells(3, c) & "Step2")).Visible = True 'Then unhide/leave visible worksheet that matches the naming convention in that column row 3 concatinated with "Step2"


Worksheets(CStr(Cells(3, c) & "PartsCatalog")).Visible = True 'Then unhide/leave visible worksheet that matches the naming convention in that column row 3 concatinated with "PartsCatalog"
End If


Next c 'Once this is done move on to next cell as determined by C variable


End Sub
Sub RunAllMacros() 'This Macro is used to run all the above macros, in order.
'This is done to try and prevent any error states in the Hide Macro by making sure that any naming issues hare fixed by the naming macro first
'This also prevens the Renaming macro from hitting an error state from trying to rename worksheets based on blank titles, which would cause duplicate worksheet names
'This is prevented by running the BlankFinder immediately before hand.
'The blank finder combined with Data Validation rules w/in the cells should prevent the vast majority of error states.
BlankFinder
Rename
Hide
End Sub

They work perfectly with one exception: When a the BlankFinder finds a Blank cell, the MsgBox pops up but continues through the loops and when using RunAllMacros not only does it continue through the loops but Rename and Hide also run. This typically isn't a problem but if more than one C is left blank then you get a VBA 400 error. I'm trying to user proof this because people in my office are leery of Macros/VBA so broken macro/vba pop ups they don't understand will cause them to quit using it entirely.

What I'd like to happen is that when BlankFinder finds a blank cell it gives an MsgBox and stops running. In RunAllMacros if Blankfinder finds a blank I want it to end all process, so it doesn't continue through the loops and doesn't run Rename or Hide. I'm sure this is possible I just can't figure out how with my limited knowledge.

Any advice would be very much appreciated.

Quick note, as I said I had help with these from here and elsewhere so I don't have answers as to why i did it this way vs. that other than it worked. I'm not married to any of these solutions. Also in the Hide Sub it says 3 to 5 instead of 3 to 17 because I'm still working on the mechanics of the PartsCatalog and so I've only created sheets for 1, 2 and 3 so I can test these out. There will eventually be 15 pairs of sheets but currently there are only 3. Thanks again
Hi SGTSparty, personally I would take a slightly different approach.

First of all I wouldn't use the RunAllMacros part at all. Instead I would call the next macro at the end of the one before it. Next I would add 2 lines to your first BlankFinder which will exit the macro if it finds a blank. Doing this would obviously prevent the subsequent macros from running. I have outlined my changes below:

BlankFinder
Rich (BB code):
Sub BlankFinder() 'Prevents blank names in PowerPackNames. Blanks cause follow-on Macros to fail and cannot be completely accounted for in Data Validation


For c = 3 To 17 'sets C as variable below for Column # for Col C (3) through Q (17)




If Cells(3, c) = "" Then 'if Cells C3-Q3 (determined by C variable) are blank then MsgBox.
        MsgBox "Power Pack Names Cannot Be Blank"
            Exit Sub
    ElseIf IsEmpty(Cells(3, c)) Then 'If cells C3-Q3 (determined by C variable) are empty then MsgBox. Not sure diff between Blank and Empty. Covering Bases.
        MsgBox "Power Pack Names Cannot Be Blank"
            Exit Sub
    Else: End If 'If cell is not blank nor empty then do nothing.
Next c 'for each "C" (the cell in each column determined by the C variable) this loop is run. If the cell is not blank or empty it moves on to the next C.

Call Rename


End Sub

Rename
Rich (BB code):
Sub Rename() 'This Macro renames each worksheet with the name in cell A1 of that sheet.
'A1 is formula that throughputs the Power Pack Name from Step1 C3-Q3. Sheet name restirctions are controlled in those cells.
'string length, special characters and duplicates are controlled using Data Validation forumlas.
'Blanks are controlled with the BlankFinder Macro above.
     
    Dim ws As Worksheet 'sets ws as shorthand for worksheet.
     
    For Each ws In Sheets 'Tells the Macro to run for ALL worksheets.
        ws.Name = ws.Range("A1") 'Since this runs for ALL worksheets, this means that Step1 and Admin also have their titles in A1. in Step1 hidden by Button.
    Next ws 'Once a sheet is renamed to whatever is in A1 moves on to next Worksheet.
     
Call Hide
     
End Sub

Hide (unchanged)
Rich (BB code):
Sub Hide() 'This macro hides worksheets that are not currently in use by the user.




For c = 3 To 5 'This sets the Variable C. C is the column number in the cell address used below. C provides the range of cells the macro will work for.
'In practicle terms this sets what sheets are being effected by this macro based on matching the sheet names to the names in C3-Q3.
'These sheets are to be hidden if the dropdown in C2-Q2 for that sheets corresponding column is set to "No" and made visible if set to "Yes"
'Ensure that the To number above is set to you column number of the last cell in Power Pack Names that has a matching Worksheet set.
'This also means that the worksheet naming convention must match that stated below. This step is handled by the Rename Macro above.




If Cells(2, c) = "No" Then 'If drop down in Row 2 for the corresponding column C is set to No
Worksheets(CStr(Cells(3, c) & "Step2")).Visible = False 'Then hide worksheet that matches the naming convention in that column row 3 concatinated with "Step2"
Worksheets(CStr(Cells(3, c) & "PartsCatalog")).Visible = False 'Then hide worksheet that matches the naming convention in that column row 3 concatinated with "PartsCatalog"
'This also means that if the worksheet naming conventions change from Step2 and PartsCatalog the macro needs to change to match.
Else
Worksheets(CStr(Cells(3, c) & "Step2")).Visible = True 'Then unhide/leave visible worksheet that matches the naming convention in that column row 3 concatinated with "Step2"




Worksheets(CStr(Cells(3, c) & "PartsCatalog")).Visible = True 'Then unhide/leave visible worksheet that matches the naming convention in that column row 3 concatinated with "PartsCatalog"
End If




Next c 'Once this is done move on to next cell as determined by C variable




End Sub
 
Upvote 0
Perfection! Thank you! I knew it was something simple but I couldn't figure out the combination. Awesome.
 
Upvote 0

Forum statistics

Threads
1,215,343
Messages
6,124,398
Members
449,155
Latest member
ravioli44

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