vba to hide rows based on cell value in specific cell

mychi11

Board Regular
Joined
May 11, 2020
Messages
82
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

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand

WhoCanDo

Board Regular
Joined
Dec 18, 2007
Messages
183
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
 

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
20,162
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows
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
 
Solution

mychi11

Board Regular
Joined
May 11, 2020
Messages
82
Office Version
  1. 2016
Platform
  1. Windows
thank you so much. This works like charm
 

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
20,162
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows
Rich (BB code):
Glad WE could help
 

mychi11

Board Regular
Joined
May 11, 2020
Messages
82
Office Version
  1. 2016
Platform
  1. Windows
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
 

Watch MrExcel Video

Forum statistics

Threads
1,129,804
Messages
5,638,455
Members
417,025
Latest member
MusterDuster

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
Top