VBA Hide all rows from a key row downwards.

ChrisMcIntyre

New Member
Joined
Jan 6, 2022
Messages
37
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi all,
Can anyone help me with the code to hide all rows downwards of a row I have a key word in please.

I have a worksheet that can have varying numbers of rows. I have hidden the word "Last" in column A301, which is the last row on the sheet in it's undedited form. What I want to do is trigger a code to hide all the rows from the one that has the word "Last" in it, downwards to the very bottom.

So as an example, if the row with the word "Last" in happens to be row 400 after the sheet has been edited (So "Last" is in A400) I need all rows from 401 to the very bottom all hidden when I click a macro button.

Hope I explained that well, apologiesif now, and I appreciate any help you can offer.
Thanks.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Hey, can you try the code below?

VBA Code:
Sub HideRows()

Dim cl As Range
Dim sh As Worksheet
Dim rng As Range

Set sh = ActiveSheet

Set cl = sh.Cells.Find(What:="Last", _
            After:=sh.Cells(1, 1), _
            LookIn:=xlValues, _
            LookAt:=xlWhole, _
            SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, _
            MatchCase:=False, _
            SearchFormat:=False)
            
        If Not cl Is Nothing Then
        
        Set rng = sh.Range(cl, cl.End(xlDown))
        rng.EntireRow.Hidden = True
        
        End If


End Sub
 
Upvote 0
Thanks for the speedy response bferraz!

It kind of worked, but it maybe hit some text I had underneath the word "Last" and so stopped hiding. I literally need it to search for the exact word "Last" in column A and then from the row below that word down, hide all rows.
 
Last edited:
Upvote 0
Maybe:
VBA Code:
Sub test()
    Rows(Cells.Find("Last", , , xlWhole).Row & ":" & Rows.Count).Hidden = True
End Sub
 
Upvote 0
Thanks so much Georgiboy, unfortunately that bugged out...

1672240478897.png
 
Upvote 0
You are welcome! Can you provide more information as the text on the row which was hidden? The argument xlWhole should check for the full text of “Last”
 
Upvote 0
I have a bunch of cells with text in relating to other lookups, but I still need all those rows hidden.
 
Upvote 0
I get that error if I put a space on the end of "Last "

The text of "Last" needs to match exactly
 
Upvote 0
100% only have "Last" in that field, no extra spaces or additional characters
 
Upvote 0
Can you try this one? This one searchs for the whole text "Last" on Column A.

VBA Code:
Sub HideRows()

Dim cl As Range
Dim sh As Worksheet
Dim rng As Range

Set sh = ActiveSheet

Set cl = sh.Range("A:A").Find(What:="Last", _
            After:=sh.Cells(1, 1), _
            LookIn:=xlValues, _
            LookAt:=xlWhole, _
            SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, _
            MatchCase:=False, _
            SearchFormat:=False)
            
        If Not cl Is Nothing Then
        
        Set rng = sh.Range(cl, cl.End(xlDown))
        rng.EntireRow.Hidden = True
        
        End If


End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,332
Messages
6,124,314
Members
449,153
Latest member
JazzSingerNL

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