Help : select rows based on cell value then hide them

jptaz

New Member
Joined
May 1, 2020
Messages
46
Office Version
  1. 2010
Platform
  1. Windows
Hello, I use this code to select rows based on the value in column U ("U1:U228").

VBA Code:
Sub SelRows()
Dim ocell As Range
Dim rng As Range

For Each ocell In Range("U1:U228")

If ocell.Value = "#v" Then

If rng Is Nothing Then

Set rng = ocell.EntireRow
Else

Set rng = Union(rng, ocell.EntireRow)
End If
End If
Next

If Not rng Is Nothing Then rng.Select

Set rng = Nothing
Set ocell = Nothing
End Sub

Then I use this code to hide the selection

VBA Code:
Sub hide1()
Selection.EntireRow.Hidden = True
End Sub

However I need to be on the right sheet (Rapport ini) for this to work
Would it be possible to edit these codes so they would work without being on the target sheet?


I've also tried this way but it is really too slow (few minutes)
VBA Code:
Sub HRows()

     Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.Calculation = xlManualCalculation
    
            
    BeginRow = 19
    EndRow = 221
    ChkCol = 21

    For RowCnt = BeginRow To EndRow
        If Cells(RowCnt, ChkCol).Value = "#v" Then
            Cells(RowCnt, ChkCol).EntireRow.Hidden = True
        Else
            Cells(RowCnt, ChkCol).EntireRow.Hidden = False
        End If
    Next RowCnt
    
      Application.ScreenUpdating = True
    Application.EnableEvents = True
    Application.Calculation = xlAutomaticCalculation
       
End Sub

Thanks for your time
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
How about ...
VBA Code:
Sub HideRows()
    Dim ocell As Range
    Dim rng As Range

    For Each ocell In Range("U1:U228")
        If ocell.Value = "#v" Then
            If rng Is Nothing Then
                Set rng = ocell
            Else
                Set rng = Union(rng, ocell)
            End If
        End If
    Next

    If Not rng Is Nothing Then rng.EntireRow.Hidden = True

    Set rng = Nothing
    Set ocell = Nothing
End Sub
 
Upvote 0
How about
VBA Code:
Sub SelRows()
Dim ocell As Range
Dim rng As Range

For Each ocell In Sheets("Rapport ini").Range("U1:U228")

If ocell.Value = "#v" Then

If rng Is Nothing Then

Set rng = ocell.EntireRow
Else

Set rng = Union(rng, ocell.EntireRow)
End If
End If
Next

If Not rng Is Nothing Then rng.EntireRow.Hidden = True

Set rng = Nothing
Set ocell = Nothing
End Sub
 
Upvote 0
Solution
Here you go, working for sheet 1

VBA Code:
Sub jec()
 Set c00 = Sheets(1).Cells(10000, 1)
 For Each it In Sheets(1).Range("U1:U228")
   If it.Value = "#v" Then Set c00 = Union(c00, it)
 Next
 If Not c00 Is Nothing Then c00.EntireRow.Hidden = True
End Sub
 
Last edited:
Upvote 0
How about
VBA Code:
Sub SelRows()
Dim ocell As Range
Dim rng As Range

For Each ocell In Sheets("Rapport ini").Range("U1:U228")

If ocell.Value = "#v" Then

If rng Is Nothing Then

Set rng = ocell.EntireRow
Else

Set rng = Union(rng, ocell.EntireRow)
End If
End If
Next

If Not rng Is Nothing Then rng.EntireRow.Hidden = True

Set rng = Nothing
Set ocell = Nothing
End Sub
Hello,

thank you very much, your solution works great! Thanks to you too GWteB!!

Have a nice day!

 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,725
Messages
6,174,128
Members
452,546
Latest member
Rafafa

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