Select Multiple Cells Depending on Value

Ottsel

Board Regular
Joined
Jun 4, 2022
Messages
174
Office Version
  1. 365
Platform
  1. Windows
I have a set range from row 4 to row 696.

I attempted this, but was unsuccessful:
VBA Code:
StartRow = 4
EndRow = 696
ColNum = 25
    For i = StartRow To EndRow
        If Cells(i, ColNum).Value <> "1" Then
            Cells(i, ColNum).EntireRow.Select
        End If
    Next i
    Selection.EntireRow.Hidden = True

I've found in my workbook selecting the cells instead of hiding each one individually speeds up the process.

Originally I was using this, but again - it takes a long time to process:
VBA Code:
    StartRow = 4
    EndRow = 696
    ColNum = 25
    For i = StartRow To EndRow
        If Cells(i, ColNum).Value = "1" Then
            Cells(i, ColNum).EntireRow.Hidden = True
        Else
            Cells(i, ColNum).EntireRow.Hidden = False
        End If
    Next i

Overall, I want to select all the rows at once, then with the selection hide it.

Any tips, tricks or ideas are greatly appreciated!
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
With this:
Application.ScreenUpdating = False
The execution is immediate

Try this:

VBA Code:
Sub hiderows()
  Dim StartRow As Long, EndRow As Long, ColNum As Long, i As Long
  Dim rng As Range
 
  StartRow = 4
  EndRow = 696
  ColNum = 25
 
  Application.ScreenUpdating = False
  Rows(StartRow & ":" & EndRow).Hidden = False
  For i = StartRow To EndRow
    If Cells(i, ColNum).Value = "1" Then
      If rng Is Nothing Then Set rng = Cells(i, ColNum) Else Set rng = Union(rng, Cells(i, ColNum))
    End If
  Next i
  If Not rng Is Nothing Then
    rng.EntireRow.Hidden = True
  End If
  Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Solution
With this:
Application.ScreenUpdating = False
The execution is immediate

Try this:

VBA Code:
Sub hiderows()
  Dim StartRow As Long, EndRow As Long, ColNum As Long, i As Long
  Dim rng As Range
 
  StartRow = 4
  EndRow = 696
  ColNum = 25
 
  Application.ScreenUpdating = False
  Rows(StartRow & ":" & EndRow).Hidden = False
  For i = StartRow To EndRow
    If Cells(i, ColNum).Value = "1" Then
      If rng Is Nothing Then Set rng = Cells(i, ColNum) Else Set rng = Union(rng, Cells(i, ColNum))
    End If
  Next i
  If Not rng Is Nothing Then
    rng.EntireRow.Hidden = True
  End If
  Application.ScreenUpdating = True
End Sub
Perfect. Thank you! I had to modify slightly, but it works much faster.
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,885
Members
452,364
Latest member
springate

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