• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Excel Hide Rows When Value Entered

padilAgrotani

New Member
I have to develop an excelsheet where a row is hidden automatically when the value is enter. There is 3 condition
An example like this:
Condition 1
If cell C16 = Value 1 to 3
Row 17 -> automatically hide row
Row 18 -> automatically hide row
Row 19 -> automatically unhide row

Condition 2
If cell C16 = Value 4 to 6
Row 17 -> automatically hide row
Row 18 -> automatically unhide row
Row 19 -> automatically hide row

Condition 3
If cell C16 = Value 7 to 9
Row 17 -> automatically unhide row
Row 18 -> automatically hide row
Row 19 -> automatically hide row
etc.
Can you please help me?
Thanks a lot
 
PadilAqrotani

Firstly, Welcome to the chandoo.org Forums

Copy this code into the Worksheet Code Module for the worksheet you want it to apply to

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Target.Address <> "$C$16" Then Exit Sub

Select Case Target
Case 1 To 3
  Rows("17:19").EntireRow.Hidden = False
  Rows("17:18").EntireRow.Hidden = True

Case 4 To 6
  Rows("17:19").EntireRow.Hidden = False
  Rows("17").EntireRow.Hidden = True
  Rows("19").EntireRow.Hidden = True

Case 7 To 9
  Rows("17:19").EntireRow.Hidden = False
  Rows("18:19").EntireRow.Hidden = True

End Select

End Sub

Enter values in C16 and see what happens

You will have to save the file as a Macro Enabled file *.xlsm
 
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Target.Address <> "$C$16" Then Exit Sub

Select Case Target
Case 1 To 3
Rows("17:19").EntireRow.Hidden = False
Rows("17:18").EntireRow.Hidden = True

Case 4 To 6
Rows("17:19").EntireRow.Hidden = False
Rows("17").EntireRow.Hidden = True
Rows("19").EntireRow.Hidden = True

Case 7 To 9
Rows("17:19").EntireRow.Hidden = False
Rows("18:19").EntireRow.Hidden = True

If Target.Address <> "$C$21" Then Exit Sub

Select Case Target
Case 1 To 3
Rows("22:24").EntireRow.Hidden = False
Rows("22:23").EntireRow.Hidden = True

Case 4 To 6
Rows("22:24").EntireRow.Hidden = False
Rows("22").EntireRow.Hidden = True
Rows("24").EntireRow.Hidden = True

Case 7 To 9
Rows("22:24").EntireRow.Hidden = False
Rows("23:24").EntireRow.Hidden = True

End Select

End Sub


I have try several times..C21 not working.
 
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Target.Address = "$C$16" Then 

Select Case Target
Case 1 To 3
Rows("17:19").EntireRow.Hidden = False
Rows("17:18").EntireRow.Hidden = True

Case 4 To 6
Rows("17:19").EntireRow.Hidden = False
Rows("17").EntireRow.Hidden = True
Rows("19").EntireRow.Hidden = True

Case 7 To 9
Rows("17:19").EntireRow.Hidden = False
Rows("18:19").EntireRow.Hidden = True

else If Target.Address = "$C$21" Then 

Select Case Target
Case 1 To 3
Rows("22:24").EntireRow.Hidden = False
Rows("22:23").EntireRow.Hidden = True

Case 4 To 6
Rows("22:24").EntireRow.Hidden = False
Rows("22").EntireRow.Hidden = True
Rows("24").EntireRow.Hidden = True

Case 7 To 9
Rows("22:24").EntireRow.Hidden = False
Rows("23:24").EntireRow.Hidden = True

End Select

end if

End Sub
 
Hi ,

Replace the entire code with this :
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
            If Target.Address = "$C$16" Then
              Select Case Target
                      Case 1 To 3
                          Rows("17:19").EntireRow.Hidden = False
                          Rows("17:18").EntireRow.Hidden = True

                      Case 4 To 6
                          Rows("17:19").EntireRow.Hidden = False
                          Rows("17").EntireRow.Hidden = True
                          Rows("19").EntireRow.Hidden = True

                      Case 7 To 9
                          Rows("17:19").EntireRow.Hidden = False
                          Rows("18:19").EntireRow.Hidden = True
              End Select

            ElseIf Target.Address = "$C$21" Then
                  Select Case Target
                          Case 1 To 3
                              Rows("22:24").EntireRow.Hidden = False
                              Rows("22:23").EntireRow.Hidden = True

                          Case 4 To 6
                              Rows("22:24").EntireRow.Hidden = False
                              Rows("22").EntireRow.Hidden = True
                              Rows("24").EntireRow.Hidden = True

                          Case 7 To 9
                              Rows("22:24").EntireRow.Hidden = False
                              Rows("23:24").EntireRow.Hidden = True
                  End Select
            End If
End Sub
Narayan
 
Hi ,

Note that I have gone with the Worksheet_SelectionChange event procedure , though the correct event to be used should be the Worksheet_Change event procedure.

In case you want the change to be made , let me know.

As it stands , you need to take the cursor to each of the mentioned cells for the rows to be hidden / unhidden. If you are in a cell such as C16 , and you change the value from one slab to another (for example if it was 2 and you changed it to 5) , then you have to move the cursor out of C16 and then bring it back to C16 for the rows to be hidden / unhidden.

If the Worksheet_Change event procedure were to be used , this would not be so ; the moment the cell value is changed , the rows will be hidden / unhidden.

Narayan
 
Another problem...help pls

For C51

Case 1 To 4
Row 52 -> automatically hide row
Row 53 -> automatically unhide row

Case 5 To 9
Row 52 -> automatically unhide row
Row 53 -> automatically hide row
 
Thanx Narayan

Padil, If you look at the structure of the code you should be able to work out how to extend it
 
Back
Top