vba to hide rows based on cell value in specific cell

mychi11

Board Regular
Joined
May 11, 2020
Messages
95
Office Version
  1. 2016
Platform
  1. Windows
Hi all,
I need to vba code to hide the row based on value in one cell. To illustrate, when the cell A29=1. I would like the row 55 to 103 to hide. When A29=2, I would like row 56 to 103 to hide so on and so forth till 50. The following code works for the first two, but as I add more code , the code fails to work. Wonder if anybody can help. Many thanks

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  1. If Range("A29").Value = 1 Then
  2. Rows("55:103").EntireRow.Hidden = True
  3. Else
  4. Rows("55:103").EntireRow.Hidden = False
  5. End If
  6. If Range("A29").Value = 2 Then
  7. Rows("56:103").EntireRow.Hidden = True
  8. Else
  9. Rows("56:103").EntireRow.Hidden = False
  10. End If
  11. End Sub
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Not sure of the reason but this works ...

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'
x = Range("A29").Value

Select Case x
Case 1: Rows("55:103").EntireRow.Hidden = False
Rows("55:103").EntireRow.Hidden = True
Case 2: Rows("55:103").EntireRow.Hidden = False
Rows("56:103").EntireRow.Hidden = True
Case Else
Rows("55:103").EntireRow.Hidden = False
End Select

End Sub
 
Upvote 0
Are you sure you want a SelectionChange event....every time you change cells the code will run ??
I would have thought a straight ChangeEvent would be better
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Rows("55:103").EntireRow.Hidden = False
x = Range("A29").Value
Select Case x
Case 1: Rows("55:103").EntireRow.Hidden = True
Case 2: Rows("56:103").EntireRow.Hidden = True
End Select

End Sub
 
Upvote 0
Solution
Rich (BB code):
Glad WE could help
 
Upvote 0
Rich (BB code):
Glad WE could help
thank you both of you. I was wondering if i can add another few lines below the line
say if B3 equals to "APPLE", i would like row 2:5 and row 6:10 to hide.
B3 equals to "ORANGE", i would like 8:11 and 9:50 to hide
 
Upvote 0

Forum statistics

Threads
1,213,527
Messages
6,114,140
Members
448,551
Latest member
Sienna de Souza

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