Change Macro from Running when Worksheet is Activated to Be Ran on Multiple Sheets By Button in 1 Sheet

doctorgresham

New Member
Joined
Aug 2, 2021
Messages
6
Office Version
  1. 365
Platform
  1. MacOS
Hi,
I currently have the following macro on several sheets in a workbook that runs each time the sheets are activated. I'm looking to change it from being ran each time I click on the individual sheets to being ran only when a button is clicked on Sheet 1. So in other words, I want to make a button on sheet 1 that will run the following macro on sheets 2, 3, 4 etc. when i press it. I'm a novice when it comes to VB, so any help/ideas would be greatly appreciated!

Private Sub Worksheet_Activate()
Dim cell As Range
For Each cell In Range("A1:A424")
If cell.Value = "0" Then
cell.EntireRow.Hidden = True
ElseIf cell.Value = "1" Then
cell.EntireRow.Hidden = False
End If
Next
End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Hi & welcome to MrExcel.
Do you want to run it on all sheets in the workbook, or just some of them?
 
Upvote 0
How many sheets do you have & how many should the code work on?
 
Upvote 0
Ok, how about
VBA Code:
Sub doctorgresham()
   Dim Ws As Worksheet
   Dim Cl As Range
   
   For Each Ws In Sheets(Array("sheet1", "sheet2", "sheet3"))
      For Each Cl In Ws.Range("A1:A424")
         Cl.EntireRow.Hidden = Cl.Value = 0
      Next Cl
   Next Ws
End Sub
Change the sheet names to suit & add the rest in the same manner.
 
Upvote 0
Solution
Thank you. Could you also include a line that will unhide the row if it's a "1". The way I designed the spreadsheet is that Column "A" is either a 1 or 0 based on a selection in sheet 1. so in setup someone could change a selection and the push the macro button and it would hide or unhide based on the new values in column "A".
 
Upvote 0
Ok cool, thanks. I'll get to plug it in and play with it during lunch. Thanks again for your help.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,222,239
Messages
6,164,779
Members
451,914
Latest member
mdfariborz

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