Home

Hide and Show Rows in a Sheet Based on a cell value

%3CLINGO-SUB%20id%3D%22lingo-sub-751531%22%20slang%3D%22en-US%22%3EHide%20and%20Show%20Rows%20in%20a%20Sheet%20Based%20on%20a%20cell%20value%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-751531%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20All%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHoping%20someone%20can%20help%20me%20out.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20would%20like%20to%20hide%20rows%20on%20a%20sheet%20based%20on%20a%20number%20in%20a%20cell%20(A6).%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EStarting%20with%20row%209%20first%2C%20so%20if%20number%201%20is%20in%20the%20A6%20cell%20then%20row%209%20is%20shown%20and%20rows%2010%20to%20258%20are%20hidden.%3C%2FP%3E%3CP%3EIf%20number%202%20is%20in%20A6%20then%20rows%209%20and%2010%20are%20shown%26nbsp%3Band%20rows%2011%20to%20258%20are%20hidden.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EContinuing%20up%20to%20number%20250%20in%20A6%20showing%20all%20rows%20up%20to%20rows.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20would%20like%20rows%201-8%20and%20all%20rows%20after%20258%20always%20visible%20and%20not%20effected%20but%20this%20formula.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20not%20completed%20a%20function%20like%20this%20before%20and%20am%20not%20having%20much%20luck%20on%20Google.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-751531%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-751587%22%20slang%3D%22en-US%22%3ERe%3A%20Hide%20and%20Show%20Rows%20in%20a%20Sheet%20Based%20on%20a%20cell%20value%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-751587%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F375132%22%20target%3D%22_blank%22%3E%40AB_2019%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3Eyou%20can%20use%20the%20following%20event%2C%20see%20attached%20example%2C%20too.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3EPrivate%20Sub%20Worksheet_Change(ByVal%20Target%20As%20Range)%3CBR%20%2F%3EDim%20rngArea%20As%20Range%3CBR%20%2F%3E%3CBR%20%2F%3EIf%20Target.Address%20%26lt%3B%26gt%3B%20%22%24A%246%22%20Then%20Exit%20Sub%3CBR%20%2F%3ERows.Hidden%20%3D%20False%3CBR%20%2F%3ESelect%20Case%20Target.Value%3CBR%20%2F%3ECase%201%3CBR%20%2F%3ESet%20rngArea%20%3D%20Range(Rows(10)%2C%20Rows(Rows.Count))%3CBR%20%2F%3ErngArea.Hidden%20%3D%20True%3CBR%20%2F%3E%3CBR%20%2F%3ECase%202%3CBR%20%2F%3ESet%20rngArea%20%3D%20Range(Rows(11)%2C%20Rows(Rows.Count))%3CBR%20%2F%3ErngArea.Hidden%20%3D%20True%3CBR%20%2F%3E%3CBR%20%2F%3ECase%203%3CBR%20%2F%3ESet%20rngArea%20%3D%20Range(Rows(12)%2C%20Rows(Rows.Count))%3CBR%20%2F%3ErngArea.Hidden%20%3D%20True%3CBR%20%2F%3E%3CBR%20%2F%3ECase%204%3CBR%20%2F%3ESet%20rngArea%20%3D%20Range(Rows(13)%2C%20Rows(Rows.Count))%3CBR%20%2F%3ErngArea.Hidden%20%3D%20True%3CBR%20%2F%3E%3CBR%20%2F%3ECase%205%3CBR%20%2F%3ESet%20rngArea%20%3D%20Range(Rows(14)%2C%20Rows(Rows.Count))%3CBR%20%2F%3ErngArea.Hidden%20%3D%20True%3CBR%20%2F%3E%3CBR%20%2F%3ECase%206%3CBR%20%2F%3ESet%20rngArea%20%3D%20Range(Rows(15)%2C%20Rows(Rows.Count))%3CBR%20%2F%3ErngArea.Hidden%20%3D%20True%3CBR%20%2F%3E%3CBR%20%2F%3ECase%207%3CBR%20%2F%3ESet%20rngArea%20%3D%20Range(Rows(16)%2C%20Rows(Rows.Count))%3CBR%20%2F%3ErngArea.Hidden%20%3D%20True%3CBR%20%2F%3E%3CBR%20%2F%3ECase%208%3CBR%20%2F%3ESet%20rngArea%20%3D%20Range(Rows(17)%2C%20Rows(Rows.Count))%3CBR%20%2F%3ErngArea.Hidden%20%3D%20True%3CBR%20%2F%3E%3CBR%20%2F%3ECase%20Else%3CBR%20%2F%3ESet%20rngArea%20%3D%20Range(Rows(8)%2C%20Rows(Rows.Count))%3CBR%20%2F%3ErngArea.Hidden%20%3D%20False%3CBR%20%2F%3EEnd%20Select%3CBR%20%2F%3E%3CBR%20%2F%3EEnd%20Sub%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBest%20regards%3C%2FP%3E%3CP%3EBernd%3C%2FP%3E%3CP%3E%3CA%20title%3D%22VBA%20database%22%20href%3D%22https%3A%2F%2Fvba-tanker.com%2F%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ethe%20vba-tanker%20-%20a%20databse%20full%20of%26nbsp%3B%20macros%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-751590%22%20slang%3D%22en-US%22%3ERe%3A%20Hide%20and%20Show%20Rows%20in%20a%20Sheet%20Based%20on%20a%20cell%20value%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-751590%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F347933%22%20target%3D%22_blank%22%3E%40Berndvbatanker%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHi%26nbsp%3B%3CSPAN%3EBernd%2C%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EThanks%20for%20your%20reply%2C%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EI%20am%20not%20sure%20if%20I%20am%20using%20this%20correctly%2C%20I%20have%20downloaded%20your%20example%20and%20have%20put%20some%20different%20numbers%20in%20the%20highlighted%20A6%20cell%20but%20it%20does%20not%20have%20any%20effect%20on%20the%20sheet%2C%20all%20rows%20remain%20visible.%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3ECan%20you%20please%20let%20me%20know%20if%20i%20am%20missing%20something.%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-751610%22%20slang%3D%22en-US%22%3ERe%3A%20Hide%20and%20Show%20Rows%20in%20a%20Sheet%20Based%20on%20a%20cell%20value%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-751610%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3CBR%20%2F%3Emacros%20are%20activated%3F%3C%2FP%3E%3CP%3ECopy%20the%20macro%20to%20a%20new%20workbook%20behind%20first%20worksheet%20and%20test%20again%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-751676%22%20slang%3D%22en-US%22%3ERe%3A%20Hide%20and%20Show%20Rows%20in%20a%20Sheet%20Based%20on%20a%20cell%20value%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-751676%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F347933%22%20target%3D%22_blank%22%3E%40Berndvbatanker%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHi%2C%20Yes%20they%20are%20on.%20I%20have%20seen%20it%20work%20in%20a%20new%20spreadsheet.%3C%2FP%3E%3CP%3E%26nbsp%3Bto%26nbsp%3B%3C%2FP%3E%3CP%3EI%20would%20like%20to%20try%20a%20different%20approach%20to%20this%20one.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECan%20you%20please%20let%20me%20know%20if%20it%20is%20possible%20to%20create%20a%20macro%20to%20apply%20to%20an%20active%20X%20toggle%20button%20to%20show%20and%20hide%20rows.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20values%20are%20%22Yes%22%20to%20show%20the%20row%20and%20%22No%22%20to%20hide%20the%20row.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThese%20are%20in%20column%20A%20rows%209%20to%20258.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-751715%22%20slang%3D%22en-US%22%3ERe%3A%20Hide%20and%20Show%20Rows%20in%20a%20Sheet%20Based%20on%20a%20cell%20value%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-751715%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F375132%22%20target%3D%22_blank%22%3E%40AB_2019%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHi%2C%20that's%20a%20good%20idea.%20Look%20at%20the%20attachment.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3EPrivate%20Sub%20ToggleButton1_Click()%3CBR%20%2F%3EDim%20rngArea%20As%20Range%3CBR%20%2F%3E%3CBR%20%2F%3ESet%20rngArea%20%3D%20Range(Rows(9)%2C%20Rows(258))%3CBR%20%2F%3EIf%20Me.ToggleButton1.Value%20%3D%20True%20Then%3CBR%20%2F%3ErngArea.Hidden%20%3D%20False%3CBR%20%2F%3EMe.ToggleButton1.Caption%20%3D%20%22Hide%22%3CBR%20%2F%3EElse%3CBR%20%2F%3ErngArea.Hidden%20%3D%20True%3CBR%20%2F%3EMe.ToggleButton1.Caption%20%3D%20%22Show%22%3CBR%20%2F%3EEnd%20If%3CBR%20%2F%3E%3CBR%20%2F%3EEnd%20Sub%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBest%20regards%3C%2FP%3E%3CP%3EBernd%3C%2FP%3E%3CP%3EMy%20website%20%3CA%20title%3D%22the%20vba%20database%22%20href%3D%22https%3A%2F%2Fvba-tanker.com%2F%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Evba-tanker.com%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
AB_2019
Occasional Contributor

Hi All,

 

Hoping someone can help me out.

 

I would like to hide rows on a sheet based on a number in a cell (A6).

 

Starting with row 9 first, so if number 1 is in the A6 cell then row 9 is shown and rows 10 to 258 are hidden.

If number 2 is in A6 then rows 9 and 10 are shown and rows 11 to 258 are hidden.

 

Continuing up to number 250 in A6 showing all rows up to rows.

 

I would like rows 1-8 and all rows after 258 always visible and not effected but this formula.

 

I have not completed a function like this before and am not having much luck on Google.

 

Thank you!

5 Replies

@AB_2019 

Hi,

you can use the following event, see attached example, too.

 

Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngArea As Range

If Target.Address <> "$A$6" Then Exit Sub
Rows.Hidden = False
Select Case Target.Value
Case 1
Set rngArea = Range(Rows(10), Rows(Rows.Count))
rngArea.Hidden = True

Case 2
Set rngArea = Range(Rows(11), Rows(Rows.Count))
rngArea.Hidden = True

Case 3
Set rngArea = Range(Rows(12), Rows(Rows.Count))
rngArea.Hidden = True

Case 4
Set rngArea = Range(Rows(13), Rows(Rows.Count))
rngArea.Hidden = True

Case 5
Set rngArea = Range(Rows(14), Rows(Rows.Count))
rngArea.Hidden = True

Case 6
Set rngArea = Range(Rows(15), Rows(Rows.Count))
rngArea.Hidden = True

Case 7
Set rngArea = Range(Rows(16), Rows(Rows.Count))
rngArea.Hidden = True

Case 8
Set rngArea = Range(Rows(17), Rows(Rows.Count))
rngArea.Hidden = True

Case Else
Set rngArea = Range(Rows(8), Rows(Rows.Count))
rngArea.Hidden = False
End Select

End Sub

 

Best regards

Bernd

the vba-tanker - a databse full of  macros

@Berndvbatanker 

Hi Bernd,

 

Thanks for your reply,

 

I am not sure if I am using this correctly, I have downloaded your example and have put some different numbers in the highlighted A6 cell but it does not have any effect on the sheet, all rows remain visible.

 

Can you please let me know if i am missing something.

Hi,
macros are activated?

Copy the macro to a new workbook behind first worksheet and test again

@Berndvbatanker 

Hi, Yes they are on. I have seen it work in a new spreadsheet.

 to 

I would like to try a different approach to this one. 

 

Can you please let me know if it is possible to create a macro to apply to an active X toggle button to show and hide rows.

 

The values are "Yes" to show the row and "No" to hide the row.

 

These are in column A rows 9 to 258.

 

Thank you.

@AB_2019 

Hi, that's a good idea. Look at the attachment.

 

Private Sub ToggleButton1_Click()
Dim rngArea As Range

Set rngArea = Range(Rows(9), Rows(258))
If Me.ToggleButton1.Value = True Then
rngArea.Hidden = False
Me.ToggleButton1.Caption = "Hide"
Else
rngArea.Hidden = True
Me.ToggleButton1.Caption = "Show"
End If

End Sub

 

Best regards

Bernd

My website vba-tanker.com

Related Conversations
Tabs and Dark Mode
cjc2112 in Discussions on
17 Replies
Stable version of Edge insider browser
HotCakeX in Discussions on
35 Replies
flashing a white screen while open new tab
cntvertex in Discussions on
11 Replies
How to Prevent Teams from Auto-Launch
chenrylee in Microsoft Teams on
28 Replies