VBA code to hide a range of cells based on cell value

georgede

New Member
Joined
Sep 13, 2019
Messages
4
Hi,
I am not sure if i am close but i am attempting to hide a range of rows based on the value of another cell. To be more clear A11 value is "Status" and i need to evaluate B11 and if it is "Open" then hide 11 rows up. This repeats in a dynamically lengthed document that i run weekly. Below is what i have tried - i am sure it is not as efficient as it could be but it is a starting point. Any help or suggestions would be greatly appreciated.

Code:
Sub Closed_Susp()
    Dim Rng As Range
    Dim WorkRng As Range
    On Error Resume Next
    xTitleID = "Status Tool"
    Set WorkRng = Application.Selection
    Set WorkRng = Application.InputBox("Range", xTitleID, WorkRng.Address, Type:=8)
    Set WorkRng = WorkRng.Columns(1)
    xLastRow = WorkRng.Rows.Count
    Application.ScreenUpdating = False
    For xRowIndex = xLastRow To 1 Step -1
        Set Rng = WorkRng.Range("A" & xRowIndex)
        If Rng.Value = "Status:" And Rng.Offset(0, 1).Value = "OPEN" Then
            Range(ActiveCell.Offset(0, 0), ActiveCell.Offset(-11, 0)).EntireRow.Hidden = True
        End If
    Next
    Application.ScreenUpdating = True
End Sub

Thank you!!
 

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
The major issue is if your selection happens to be above row 11, you'll crash since you can't hide rows that don't exist (well, I guess you won't because you have the resume next, but that's not true error handling). ie, the Status and Open are in row 10, you try to hide a row above row 1, which does not exist. Or even if the row is 11, then you're trying to hide row 0... so you might want to ensure the selection is below row 11.

Also, your string comparisons are case dependent. So if the value in B is "Open" or "open" - it won't match.

else, sure, code is rough, but we all start somewhere.
 
Upvote 0
@starl thank you for your reply. This is from a generated report so there will always be a minimum of 11 lines above the Status line, and also the status value is generated so it will have the same capitalization (a good point out however). With all that in mind, my problem is that for some reason the code above does not actually hide anything - and that is my issue.

I assumed it was something in my code that i am missing, bad argument or a misunderstanding of the application of the operators. Any thoughts would be greatly appreciated.

Thank you for your time and help!
 
Upvote 0
What is the exact value in col A?
"Status", or "Status:" or maybe "Status: something"
 
Upvote 0
Your code is fine and works (in a perfectly setup workbook). But, like Fluff pointed out - Excel makes EXACT comparisons. Even a space can throw of the comparison. Step through your code, when it reaches a line you KNOW is correct, watch the variables, check the logic and see what's throwing it off.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,155
Members
453,021
Latest member
Justyna P

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