Automatically hide/unhide rows

bbugs73

New Member
Joined
Jan 29, 2025
Messages
3
Office Version
  1. 365
Platform
  1. Windows
I have a simple spreadsheet that contains a field where a user will populate a number of months. Below that, there is a row that represents each month. If the user enters 24, which is the max, the entire set of rows is shown. However, when the user enters 15, there are 9 rows with zeroes. So, I am looking to hide those rows where the field is zero. Using that same sheet, if the user then changes the number of months to 18, then that number of rows should be seen. Please help!
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
This is certainly doable. Are you looking for a VBA or formula solution? To offer anything of use, we would need to know a lot more about your layout. Are you able to post a copy of your sheet using the XL2BB - Excel Range to BBCode? Or alternatively, share your file via Google Drive, Dropbox or similar file sharing platform.
 
Upvote 0
I attached a screen shot and included the basic formula I am using. You will see lines 27 - 32, months 19-24 are zero and this I would like to hide. If the user then enters 24, they would be unhidden. As far as solution option, either is fine. I plan on sending this out to others to use so whichever is easier.
 

Attachments

  • Screenshot 2025-01-29 at 9.24.10 PM.png
    Screenshot 2025-01-29 at 9.24.10 PM.png
    178.6 KB · Views: 6
Upvote 0
There's a number of different ways you could do this. I'll offer 3 options here.
1. VBA
Right click the sheet tab name, select View Code, and place the following module in the code window that appears on the right of screen. You'll need to save the file as macro-enabled or binary. I've used a data validation rule in cell C7 to only accept whole numbers from 1 to 24.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Range("C7"), Target) Is Nothing Then
        On Error GoTo Escape
        Application.EnableEvents = False
        Application.ScreenUpdating = False
        Rows("9:32").Hidden = True
        Range("B9").Resize(Target).EntireRow.Hidden = False
    End If
    
Continue:
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    Exit Sub
Escape:
    MsgBox "Error " & Err.Number & ": " & Err.Description
    Resume Continue
End Sub

Result when you enter 15:
hide rows 3 ways.xlsm
ABCD
1
2
3Option 1 - VBA
4Date:
5Name:
6Balance:2000000
7# of month15
8
91133333
102133333
113133333
124133333
135133333
146133333
157133333
168133333
179133333
1810133333
1911133333
2012133333
2113133333
2214133333
2315133333
33
Option 1 - VBA
Cell Formulas
RangeFormula
C9:C23C9=IF(B9<=$C$7,$C$6/$C$7,0)
Cells with Data Validation
CellAllowCriteria
C7Whole numberbetween 1 and 24


2. Conditional Formatting
This doesn't hide the rows, but turns the font white so as to 'disappear'. When you enter 19 in C7 it looks like this:
hide rows 3 ways.xlsm
ABCD
1
2
3Date:
4Name:
5Balance:2000000
6# of month19
7
81105263
92105263
103105263
114105263
125105263
136105263
147105263
158105263
169105263
1710105263
1811105263
1912105263
2013105263
2114105263
2215105263
2316105263
2417105263
2518105263
2619105263
27200
Option 2 - Conditional Format
Cell Formulas
RangeFormula
C8:C27C8=IF(B8<=$C$6,$C$5/$C$6,0)
Cells with Conditional Formatting
CellConditionCell FormatStop If True
B8:C31Expression=$B8>$C$6textYES
Cells with Data Validation
CellAllowCriteria
C6Whole numberbetween 1 and 24


3. Filter function. Your profile says you have 365, so you could use the Filter function like this - only returns the relevant rows but requires a secondary range: When you enter 22 in C7 you get this: (you could place the filtered range on another sheet if you wanted)
hide rows 3 ways.xlsm
ABCDEFG
1
2
3Option 3 - Filter formula
4Date:Date:
5Name:Name:
6Balance:2000000Balance:2000000
7# of month22# of month22
8
9190909190909
10290909290909
11390909390909
12490909490909
13590909590909
14690909690909
15790909790909
16890909890909
17990909990909
1810909091090909
1911909091190909
2012909091290909
2113909091390909
2214909091490909
2315909091590909
2416909091690909
2517909091790909
2618909091890909
2719909091990909
2820909092090909
2921909092190909
3022909092290909
31230
32240
33
Option 3 - Filter formula
Cell Formulas
RangeFormula
F6:F7F6=C6
E9:F30E9=FILTER(B9:C32,B9:B32<=F7,"")
C9:C32C9=IF(B9<=$C$7,$C$6/$C$7,0)
Dynamic array formulas.
Cells with Data Validation
CellAllowCriteria
C7Whole numberbetween 1 and 24
F7Whole numberbetween 1 and 24
 
Upvote 0
Try this worksheet event. If the value entered is >24 then C7="" with the message.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$7" Then
Application.EnableEvents = False

If Target <= 24 Then
Rows("9:32").Hidden = False
Rows(9 + Target & ":" & 32).EntireRow.Hidden = True
Else
MsgBox "Value should be <= 24"
Target = ""
Rows("9:32").Hidden = False
End If
Application.EnableEvents = True

End If
End Sub
How to use worksheet event the code
Right click on Sheet tab --> view code
Visual Basic (VB) window opens.
Select worksheet in drop down instead of General
Paste the code
Close the VB window.
Save the file as .xlsm
 
Upvote 0
There's a number of different ways you could do this. I'll offer 3 options here.
1. VBA
Right click the sheet tab name, select View Code, and place the following module in the code window that appears on the right of screen. You'll need to save the file as macro-enabled or binary. I've used a data validation rule in cell C7 to only accept whole numbers from 1 to 24.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Range("C7"), Target) Is Nothing Then
        On Error GoTo Escape
        Application.EnableEvents = False
        Application.ScreenUpdating = False
        Rows("9:32").Hidden = True
        Range("B9").Resize(Target).EntireRow.Hidden = False
    End If
   
Continue:
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    Exit Sub
Escape:
    MsgBox "Error " & Err.Number & ": " & Err.Description
    Resume Continue
End Sub

Result when you enter 15:
hide rows 3 ways.xlsm
ABCD
1
2
3Option 1 - VBA
4Date:
5Name:
6Balance:2000000
7# of month15
8
91133333
102133333
113133333
124133333
135133333
146133333
157133333
168133333
179133333
1810133333
1911133333
2012133333
2113133333
2214133333
2315133333
33
Option 1 - VBA
Cell Formulas
RangeFormula
C9:C23C9=IF(B9<=$C$7,$C$6/$C$7,0)
Cells with Data Validation
CellAllowCriteria
C7Whole numberbetween 1 and 24


2. Conditional Formatting
This doesn't hide the rows, but turns the font white so as to 'disappear'. When you enter 19 in C7 it looks like this:
hide rows 3 ways.xlsm
ABCD
1
2
3Date:
4Name:
5Balance:2000000
6# of month19
7
81105263
92105263
103105263
114105263
125105263
136105263
147105263
158105263
169105263
1710105263
1811105263
1912105263
2013105263
2114105263
2215105263
2316105263
2417105263
2518105263
2619105263
27200
Option 2 - Conditional Format
Cell Formulas
RangeFormula
C8:C27C8=IF(B8<=$C$6,$C$5/$C$6,0)
Cells with Conditional Formatting
CellConditionCell FormatStop If True
B8:C31Expression=$B8>$C$6textYES
Cells with Data Validation
CellAllowCriteria
C6Whole numberbetween 1 and 24


3. Filter function. Your profile says you have 365, so you could use the Filter function like this - only returns the relevant rows but requires a secondary range: When you enter 22 in C7 you get this: (you could place the filtered range on another sheet if you wanted)
hide rows 3 ways.xlsm
ABCDEFG
1
2
3Option 3 - Filter formula
4Date:Date:
5Name:Name:
6Balance:2000000Balance:2000000
7# of month22# of month22
8
9190909190909
10290909290909
11390909390909
12490909490909
13590909590909
14690909690909
15790909790909
16890909890909
17990909990909
1810909091090909
1911909091190909
2012909091290909
2113909091390909
2214909091490909
2315909091590909
2416909091690909
2517909091790909
2618909091890909
2719909091990909
2820909092090909
2921909092190909
3022909092290909
31230
32240
33
Option 3 - Filter formula
Cell Formulas
RangeFormula
F6:F7F6=C6
E9:F30E9=FILTER(B9:C32,B9:B32<=F7,"")
C9:C32C9=IF(B9<=$C$7,$C$6/$C$7,0)
Dynamic array formulas.
Cells with Data Validation
CellAllowCriteria
C7Whole numberbetween 1 and 24
F7Whole numberbetween 1 and 24
The VBA code worked perfectly, thank you so much!
 
Upvote 0

Forum statistics

Threads
1,226,116
Messages
6,189,057
Members
453,523
Latest member
Don Quixote

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