Apply VBA on specific sheets

darcypred

New Member
Joined
Feb 1, 2021
Messages
3
Office Version
  1. 2010
Platform
  1. Windows
Hi everyone,
I don't know much about VBA, but I am trying to hide rows on multiple sheets without having to go through each sheet and run the macro. Here is my code:
Sub HideRows()
Dim cell As Range
For Each cell In Range("A1:A30")
If UCase(cell.Value) = "NO" Then
cell.EntireRow.Hidden = True
End If
Next
End Sub
And I want to run this on sheets 1, 27, 28, 31, 32, 37, 38, 39, and 45.
Thank you for you help!
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Hi & welcome to MrExcel.
Are those the sheet names, or the index numbers?
 
Upvote 0
Welcome to the Board!

Try this:
VBA Code:
Sub HideRows()

    Dim cell As Range
    Dim shts
    Dim i As Long
    
    Application.ScreenUpdating = False
    
    shts = Array(1, 27, 28, 31, 32, 37, 38, 39, 45)
    
    For i = LBound(shts) To UBound(shts)
        Sheets(shts(i)).Activate
        For Each cell In Range("A1:A30")
            If UCase(cell.Value) = "NO" Then
                cell.EntireRow.Hidden = True
            End If
        Next cell
    Next i
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Welcome to the Board!

Try this:
VBA Code:
Sub HideRows()

    Dim cell As Range
    Dim shts
    Dim i As Long
   
    Application.ScreenUpdating = False
   
    shts = Array(1, 27, 28, 31, 32, 37, 38, 39, 45)
   
    For i = LBound(shts) To UBound(shts)
        Sheets(shts(i)).Activate
        For Each cell In Range("A1:A30")
            If UCase(cell.Value) = "NO" Then
                cell.EntireRow.Hidden = True
            End If
        Next cell
    Next i
   
    Application.ScreenUpdating = True
   
End Sub
I input this and it hid rows on Sheet 1 but not on any of the other sheets
 
Upvote 0
Hi Darcy,​
as it could be easier to help with an attachment (dropbox or any other files hosting website) …​
 
Upvote 0
I input this and it hid rows on Sheet 1 but not on any of the other sheets
Where exactly did you place the code?
Did you put it in a General Module, or the Sheet1 module?
It should be in a General Module.
 
Upvote 0
Hi,
try this update to your code and see if helps

VBA Code:
Sub HideRows()

    Dim cell    As Range
    Dim i       As Long
    
    For i = 1 To 9
        With Worksheets(Choose(i, 1, 27, 28, 31, 32, 37, 38, 39, 45))
        For Each cell In .Range("A1:A30")
                cell.EntireRow.Hidden = UCase(cell.Value) = "NO"
        Next cell
        End With
    Next i
End Sub

Code assumes that the numbers you have provided are the sheets index number

If though, they are the sheets name then try this version

VBA Code:
Sub HideRows()

    Dim cell    As Range
    Dim i       As Long
    
    For i = 1 To 9
        With Worksheets(CStr(Choose(i, 1, 27, 28, 31, 32, 37, 38, 39, 45)))
        For Each cell In .Range("A1:A30")
                cell.EntireRow.Hidden = UCase(cell.Value) = "NO"
        Next cell
        End With
    Next i
End Sub

Hope helpful

Dave
 
Upvote 0

Forum statistics

Threads
1,214,891
Messages
6,122,105
Members
449,066
Latest member
Andyg666

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