VBA Macro to hide rows based on column

biker12345

New Member
Joined
Aug 20, 2018
Messages
4
So I have two worksheets (Sheet1 and Sheet2). I need to hide the row if the value in column2 = 0. I have found the code to hide the rows (see below), but the code takes forever to run (since it checks every row) and I have to run the code from each Sheet1 and Sheet2. Is there a way I can stay on Sheet3 (or any other sheet), click a button which runs a macro that hides rows in both Sheet1 and Sheet2 automatically? Thank you!

Current code below:


Sub Row_Hide()


BeginRow = 14
EndRow = 701
ChkCol = 2



For RowCnt = BeginRow To EndRow
If Cells(RowCnt, ChkCol).Value = 0 Then
Cells(RowCnt, ChkCol).EntireRow.Hidden = True
Else
Cells(RowCnt, ChkCol).EntireRow.Hidden = False
End If
Next RowCnt




End Sub
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
I figured out how to make it cycle through the two sheets, but it still takes a long time to run. How can I speed this up?

Sub Row_Hide()


BeginRow = 14
EndRow = 701
ChkCol = 2
sheetlist = Array("Sheet1", "Sheet2")

For i = LBound(sheetlist) To UBound(sheetlist)
Worksheets(sheetlist(i)).Activate


For RowCnt = BeginRow To EndRow
If Cells(RowCnt, ChkCol).Value = 0 Then
Cells(RowCnt, ChkCol).EntireRow.Hidden = True
Else
Cells(RowCnt, ChkCol).EntireRow.Hidden = False
End If
Next RowCnt

Next

End Sub
 
Upvote 0
.
Code:
Option Explicit


Sub Row_Hide()
Dim RowCnt As Long
Dim BeginRow As Integer
Dim EndRow As Integer
Dim ChkCol As Integer
Dim i As Integer
Dim Count1 As Integer


Count1 = ActiveWorkbook.Worksheets.Count


BeginRow = 14
EndRow = 701
ChkCol = 2
Application.ScreenUpdating = False


For i = 1 To 2  '<-- edit this to include more than two sheets. Ex: 1 to 4 for first four sheets.


    Worksheets(i).Activate
            For RowCnt = BeginRow To EndRow
                If Cells(RowCnt, ChkCol).Value = 0 Then
                    Cells(RowCnt, ChkCol).EntireRow.Hidden = True
                Else
                    Cells(RowCnt, ChkCol).EntireRow.Hidden = False
                End If
            Next RowCnt
Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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