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

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
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,214,649
Messages
6,120,731
Members
448,987
Latest member
marion_davis

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