Automaticallly hide specific rows based on single cell value

JoelHedberg

New Member
Joined
Jul 3, 2019
Messages
5
Hello everybody, I've run into some problems with a worksheet that I'm doing during my internship. The idea is that based on the value of the cell CU2 I wan to hide rows with values greater than the cell value. So if CU2=3 I want to hide the rows 4-9 and so on. The easiset thing is probably to write a macro that does this but I've never touched VBA so it's pretty difficult for me to come up with a macro. I'll make sure to leave a picture to farther clarify what I'm wanting to do. Grateful for any feedback that I receive.

Joel
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
bf4ea01b59bce423a8e0600c799a940e-full.png
 
Upvote 0
Hello everybody, I've run into some problems with a worksheet that I'm doing during my internship. The idea is that based on the value of the cell CU2 I wan to hide rows with values greater than the cell value. So if CU2=3 I want to hide the rows 4-9 and so on. The easiset thing is probably to write a macro that does this but I've never touched VBA so it's pretty difficult for me to come up with a macro. I'll make sure to leave a picture to farther clarify what I'm wanting to do. Grateful for any feedback that I receive.

Joel
Hi ,
if CU2=3 then hide rows 4-9 is OK what does that so on means....
 
Last edited:
Upvote 0
I just meant that I want to hide all the rows with values higher than the value in CU2. So if CU2=1 rows 2-9 should be hidden, if CU2=2 rows 3-9 should be hidden. So all the rows with value greater than CU2 shall be hidden. I also want to unhide the rows if the value of CU2 changes back to for example 1.
 
Upvote 0
I just meant that I want to hide all the rows with values higher than the value in CU2. So if CU2=1 rows 2-9 should be hidden, if CU2=2 rows 3-9 should be hidden. So all the rows with value greater than CU2 shall be hidden. I also want to unhide the rows if the value of CU2 changes back to for example 1.
 
Upvote 0
All the rows means is your sheet has only 9 rows

If yes, below code will work
Code:
Sub JoelHedberg()
        If Range("CU2").Value = 1 Then
            Rows("2:9").EntireRow.Hidden = False
        ElseIf Range("CU2").Value = 2 Then
            Rows("3:9").EntireRow.Hidden = True
        ElseIf Range("CU2").Value = 3 Then
            Rows("4:9").EntireRow.Hidden = True
        ElseIf Range("CU2").Value = 4 Then
            Rows("5:9").EntireRow.Hidden = True
        ElseIf Range("CU2").Value = 5 Then
            Rows("6:9").EntireRow.Hidden = True
        ElseIf Range("CU2").Value = 6 Then
            Rows("7:9").EntireRow.Hidden = True
        ElseIf Range("CU2").Value = 7 Then
            Rows("8:9").EntireRow.Hidden = True
        End If
End Sub
 
Last edited:
Upvote 0
Hi Girish, thank you very much for the rapid response. The picture that I linked is actually just a small part of the whole document, but I only want the part in the picture to be hidden/visible. The clarify: I have about 40 rows in total in the sheet but the rows that I want to be affected by the macro are CS8:CS18 stopping at CY8:CY18.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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