VBA to hide sheets based on dynamic cell values

RoryColtman

New Member
Joined
Jul 16, 2019
Messages
1
Hi - relatively new to VBA and needassistance!
I have a list of around 50 cell values that sum up columns in correspondingsheets in the same workbook, I would like to hide the particular sheet based onthe value of the cell.
In the example below I would like to hide sheet 1 only, but should the valuedrop to below 0 in sheet 2 I would like to hide sheet 2 as well.

Sheet name Sum of column A in sheet
Sheet1 -1000
Sheet2 500
Sheet3 300
Sheet4 600

I hope this makes sense! Tks












 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Try this
- code below loop all sheets in workbook, finds sheet name in column A and uses value in column C to determine if visible \ hidden
- On Error Resume Next prevents code failing if sheet name is not in list etc

Sheet Name = "Index"
Column A : List of Sheet Names
Column B : values associated with sheet
Column C : Different formula in each cell returning True\False to tell VBA if sheet should be visible\hidden

Excel 2016 (Windows) 32 bit
A
B
C
D
1
Sheet NameValueVisible ? Formula in column C
2
Sheet1
1000​
FALSE​
=B2>1000
3
Sheet2
500​
TRUE​
=B3>=0
4
Sheet3
300​
FALSE​
=B4<200
5
Sheet4
600​
TRUE​
=B5<=1000
6
Sheet: Index

Amend to match your requirements
Code:
Sub HideSheetsBasedOnRules()
    Dim ws As Worksheet, rng As Range
    With Sheets("[COLOR=#ff0000]Index[/COLOR]")
        Set rng = .Range("A2", .Range("A" & Rows.Count).End(xlUp))
    End With
        
    On Error Resume Next
    For Each ws In ThisWorkbook.Sheets
        ws.Visible = rng.Find(ws.Name).Offset(, 2).Value
    Next
End Sub
 
Upvote 0
I guess the names start in cell A2.
Run this macro on the sheet where you have the sheet-sum relationship


Code:
Sub test()
    Dim c As Range
    On Error Resume Next
    For Each c In Range("[COLOR=#0000ff]A2[/COLOR]", Range("A" & Rows.Count).End(xlUp))
        If c.Offset(0, 1) < 0 Then Sheets(c.Value).Visible = 0
    Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,556
Messages
6,114,284
Members
448,562
Latest member
Flashbond

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