VBA to remove 0 values on multiple sheets

rrossi

New Member
Joined
Aug 18, 2021
Messages
6
Office Version
  1. 365
Platform
  1. Windows
How do I edit the programming below to perform same process for different worksheets by using one macro?

Sub Format_report()
'
' Format_report Macro
'

ActiveWorkbook.RefreshAll
ActiveSheet.AutoFilter.ApplyFilter

Dim c As Range

For Each c In Range("U1:AD1").Cells
If c.Value = "Hide" Then
c.EntireColumn.Hidden = True

End If
Next c

Dim cell As Range
For Each cell In Range("c7:c532")
If cell <> "" Then
If cell = 0 Then cell.EntireRow.Hidden = True

End If
Next cell

Sheets("Sheet1").Select
Range("C1").Select

MsgBox "Formatting Completed! Report Ready!"

End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Hi & welcome to MrExcel.

On all the sheets of the book, or just some?

For all sheets:

VBA Code:
Sub Format_report()
'
' Format_report Macro
'
  Dim sh As Worksheet
  Dim c As Range
  
  Application.ScreenUpdating = False
  ActiveWorkbook.RefreshAll
  ActiveSheet.AutoFilter.ApplyFilter
  
  For Each sh In Sheets
    For Each c In sh.Range("U1:AD1").Cells
      If c.Value = "Hide" Then
        c.EntireColumn.Hidden = True
      End If
    Next c
    For Each c In sh.Range("c7:c532")
      If c <> "" Then
        If c = 0 Then c.EntireRow.Hidden = True
      End If
    Next c
  Next sh
  Sheets("Sheet1").Select
  Range("C1").Select
  MsgBox "Formatting Completed! Report Ready!"
End Sub
 
Last edited:
Upvote 0
Just some. There will be 5 sheets. I was expecting to have to list the sheets names. I think that I would need to activate each sheet, perform the functions, and deactivate the sheet so they are not grouped.
 
Upvote 0
Just some. There will be 5 sheets. I was expecting to have to list the sheets names. I think that I would need to activate each sheet, perform the functions, and deactivate the sheet so they are not grouped.
And thank you so much for your help!
 
Upvote 0
Just some
Try this:

VBA Code:
Sub Format_report()
'
' Format_report Macro
'
  Dim sh As Worksheet
  Dim c As Range
  
  Application.ScreenUpdating = False
  ActiveWorkbook.RefreshAll
  ActiveSheet.AutoFilter.ApplyFilter
  
  For Each sh In Sheets
    Select Case sh.Name
      Case "Sheet1", "Sheet2", "Sheet3", "etc"
        For Each c In sh.Range("U1:AD1").Cells
          If c.Value = "Hide" Then
            c.EntireColumn.Hidden = True
          End If
        Next c
        For Each c In sh.Range("c7:c532")
          If c <> "" Then
            If c = 0 Then c.EntireRow.Hidden = True
          End If
        Next c
    End Select
  Next sh
  Sheets("Sheet1").Select
  Range("C1").Select
  MsgBox "Formatting Completed! Report Ready!"
End Sub
 
Upvote 0
Im glad to help you, thanks for the feedback.
 
Upvote 0
Worked great! Thank you for all your help!
Well, it worked one time and then gave me the following error when I ran it the second time:

Run time error '91'
Object variable or block variable not set

Here is the macro with the actual tab names:

Sub Format_report()
'
' Format_report Macro
'
Dim sh As Worksheet
Dim c As Range

Application.ScreenUpdating = False
ActiveWorkbook.RefreshAll
ActiveSheet.AutoFilter.ApplyFilter

For Each sh In Sheets
Select Case sh.Name
Case "LP Weekly Report", "PA Weekly Report", "PV Weekly Report", "PW Weekly Report", "SJ Weekly Report"
For Each c In sh.Range("U1:AD1").Cells
If c.Value = "Hide" Then
c.EntireColumn.Hidden = True
End If
Next c
For Each c In sh.Range("c7:c532")
If c <> "" Then
If c = 0 Then c.EntireRow.Hidden = True
End If
Next c
End Select
Next sh
Sheets("Consolidated Weekly Report").Select
Range("C1").Select
MsgBox "Formatting Completed! Report Ready!"
End Sub

Any thoughts on how to correct? Thank you!
 
Upvote 0
Delete this line:
VBA Code:
ActiveSheet.AutoFilter.ApplyFilter
 
Upvote 0
Solution

Forum statistics

Threads
1,215,636
Messages
6,125,952
Members
449,276
Latest member
surendra75

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