Can a Button affect the code of another sheet?

riedyp

Board Regular
Joined
Feb 13, 2020
Messages
88
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have a very large workbook of sheets that contain macros that causes it to run very slow at times. However, only certain sheets need to be used at a time. I am wondering if I can make a button that when selected hides certain sheets and also comment blocks the code in those sheets so my workbook can run faster.

Example:
Home Tab(Sheet1)
Group1(Sheets 2-5)
Group2((Sheets6-10)

I want the "Home Tab" to contain 2 buttons "Group1" and "Group2".
If "Group1" is selected then Sheets6-10 are hidden and there code is comment blocked.
If "Group2" is selected then Sheets2-5 are hidden and there code is comment blocked.


Is this possible to do? If so would it actually make my documents run faster?

Thank you.
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
code only runs when triggered, and to comment out code would be very complex i believe. hiding sheets dosen't make the book quicker. Macros that trigger refresh all, and sheet formulas are more likely to be suspect
 
Upvote 0
If you hide the worksheets, you will also need to unhide them.
So instead of 2 buttons, you will need 4 buttons:
Button 1 - Hide Group 1
Button 2 - Hide Group 2
Button 3 - Unhide Group 1
Button 4 - Unhide Group 2

(You can actually use just 2 buttons, by changing and reading the buttons' captions, but I will keep simple here)

Here are the 4 codes.
Associate the appropriate code to each button:

VBA Code:
Sub HideGrp1()
    With ActiveWorkbook
        .Sheets("Sheet2").Visible = False
        .Sheets("Sheet3").Visible = False
        .Sheets("Sheet4").Visible = False
        .Sheets("Sheet5").Visible = False
        .Sheets("Sheet1").Activate
    End With
End Sub

Sub HideGrp2()
    With ActiveWorkbook
        .Sheets("Sheet6").Visible = False
        .Sheets("Sheet7").Visible = False
        .Sheets("Sheet8").Visible = False
        .Sheets("Sheet9").Visible = False
        .Sheets("Sheet10").Visible = False
        .Sheets("Sheet1").Activate
    End With
End Sub

Sub UnhideGrp1()
    With ActiveWorkbook
        .Sheets("Sheet2").Visible = True
        .Sheets("Sheet3").Visible = True
        .Sheets("Sheet4").Visible = True
        .Sheets("Sheet5").Visible = True
        .Sheets("Sheet1").Activate
    End With
End Sub

Sub UnhideGrp2()
    With ActiveWorkbook
        .Sheets("Sheet6").Visible = True
        .Sheets("Sheet7").Visible = True
        .Sheets("Sheet8").Visible = True
        .Sheets("Sheet9").Visible = True
        .Sheets("Sheet10").Visible = True
        .Sheets("Sheet1").Activate
    End With
End Sub
 
Upvote 0
Codes running slow can be caused by several reasons:
* links to other workbooks
* lookup from other workbooks
...and many other possibilities
etc.
 
Upvote 0
code only runs when triggered, and to comment out code would be very complex i believe. hiding sheets dosen't make the book quicker. Macros that trigger refresh all, and sheet formulas are more likely to be suspect
Is there a way to stop those codes from running by using a button?
 
Upvote 0
If you hide the worksheets, you will also need to unhide them.
So instead of 2 buttons, you will need 4 buttons:
Button 1 - Hide Group 1
Button 2 - Hide Group 2
Button 3 - Unhide Group 1
Button 4 - Unhide Group 2

(You can actually use just 2 buttons, by changing and reading the buttons' captions, but I will keep simple here)

Here are the 4 codes.
Associate the appropriate code to each button:

VBA Code:
Sub HideGrp1()
    With ActiveWorkbook
        .Sheets("Sheet2").Visible = False
        .Sheets("Sheet3").Visible = False
        .Sheets("Sheet4").Visible = False
        .Sheets("Sheet5").Visible = False
        .Sheets("Sheet1").Activate
    End With
End Sub

Sub HideGrp2()
    With ActiveWorkbook
        .Sheets("Sheet6").Visible = False
        .Sheets("Sheet7").Visible = False
        .Sheets("Sheet8").Visible = False
        .Sheets("Sheet9").Visible = False
        .Sheets("Sheet10").Visible = False
        .Sheets("Sheet1").Activate
    End With
End Sub

Sub UnhideGrp1()
    With ActiveWorkbook
        .Sheets("Sheet2").Visible = True
        .Sheets("Sheet3").Visible = True
        .Sheets("Sheet4").Visible = True
        .Sheets("Sheet5").Visible = True
        .Sheets("Sheet1").Activate
    End With
End Sub

Sub UnhideGrp2()
    With ActiveWorkbook
        .Sheets("Sheet6").Visible = True
        .Sheets("Sheet7").Visible = True
        .Sheets("Sheet8").Visible = True
        .Sheets("Sheet9").Visible = True
        .Sheets("Sheet10").Visible = True
        .Sheets("Sheet1").Activate
    End With
End Sub
Okay thank you very much.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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