Unhide specified number of rows

Giofan

New Member
Joined
Apr 4, 2012
Messages
14
Hi,

I am very new to VBA, so please be gentle.

I am looking for some code that will unhide a specified number of rows, dependant on a Cell value. For Example,

If "B2" states 2, then 2 rows will become unhidden, starting from row 2 inclusive (2:3). If "B2" states 5, then 5 rows will become unhidden, starting from row 2 inclusive (2:6).

Thanks for your patience.

Giofan
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
If row 2 is hidden, how are you changing the value in B2?
 
Upvote 0
OK, try this
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.CountLarge > 1 Then Exit Sub
   If Not Target.Address = "$B$1" Then Exit Sub
   Rows("2:" & Target.Value + 1).Hidden = False
End Sub
This needs to go in the relevant sheet module
 
Upvote 0
Hi Fluff,

Thanks so mych. I have copied in to the relevant module, however I am getting a 'Run-Time error '424': Object required' error; an ideas what this could be?

Many thanks in advance.

Giofan
 
Upvote 0
When you click debug, which line of code is highlighted?
 
Upvote 0
Hi Fluff,

Sorry, it didn't recognise the Sub in the Macro Box (assuming this is because it is a Private Sub), and so when I go to run the code, it prompts me to save it, which starts a New Sub. Apologies if I am making basic errors here. The error only happens if I make the Private Sub a Sub and change the (ByVal Target as Range) to (), as without it still doesn't recognise as a Sub.

Apologies for my ramblings, hope you can help. No worries if not. Thanks again in advance.

Giofan
 
Upvote 0
Leave it exactly as it was & when you change the value in B1 the rows should automatically unhide.
 
Upvote 0

Forum statistics

Threads
1,214,430
Messages
6,119,438
Members
448,897
Latest member
dukenia71

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