Excel VBA to hide a certain number of hide rows on one sheet, columns on another, based on number

gtirrell

New Member
Joined
Jun 16, 2022
Messages
13
Office Version
  1. 365
Platform
  1. Windows
I want to hide a certain number of rows on one sheet, and columns in another, based on the number in a certain cell. I have 30 rows labeled as 1-30 in A11:A40 on Sheet1 and 30 columns on Sheet2 labeled as 1-30 in F9:AI9. When you enter a number in a certain cell (e.g. "7" in cell C8), I want it to hide rows 8-30 on Sheet1 and columns 8-30 on Sheet2.

This works for Sheet1, but I don't know how to reference another sheet or have it simultaneously apply to columns:

Sub Hide_Rows_Columns()

Dim c As Range
For Each c In Range("A11:A40").Cells
If c.Value > Range("C8") Then
c.EntireRow.Hidden = True

End If
Next c

End Sub

Also, can this work just by entering the value in C8 and not having to press run on the macro?
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Hi & welcome to MrExcel.
On what sheet are you entering the number into C8?
 
Upvote 0
Ok how about
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.CountLarge > 1 Then Exit Sub
   If Target.Address(0, 0) = "C8" Then
      Rows("11:40").Hidden = False
      If Target.Value < 30 Then Range("A10").Offset(Target.Value + 1).Resize(30 - Target.Value).EntireRow.Hidden = True
      With Sheets("Sheet2")
         .Range("F9:AI9").EntireColumn.Hidden = False
         If Target.Value < 30 Then .Range("E9").Offset(, Target.Value + 1).Resize(, 30 - Target.Value).EntireColumn.Hidden = True
      End With
   End If
End Sub
This needs to go into the Sheet1 code module.
 
Upvote 0
Solution
I must be missing something pretty basic. I put my original code into the Sheet1 code module and pasted your solution after it. I also tried doing your solution alone, but unfortunately couldn't get anything to happen on Sheet2.
 
Upvote 0
Did you get any errors?
Also did the code work on sheet1?
 
Upvote 0
I think my original code is still running on Sheet1. If I delete the original code and just run your solution, I just a popup asking for the Macro Name to run, which is all blank.
 
Upvote 0
If you have put the code in the sheet module, then all you need to-do is change C8 & it will run automatically
 
Upvote 0
Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: Excel VBA to hide a certain number of hide rows on one sheet, columns on another
If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0

Forum statistics

Threads
1,215,327
Messages
6,124,292
Members
449,149
Latest member
mwdbActuary

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