VBA: Use Cell Value as Sheet reference to Hide/Unhide Sheets based on Cell Value

rickytb

New Member
Joined
Jul 18, 2018
Messages
15
Hi there,

Hopefully the title is correctly describing what I need, but here I will elaborate.
I will for example have the following worksheets:
- Overview
- Adam
- John
- Nick
- Tess

On the Overview sheet you will find the total of expenses per employee. The name in column A and the value in column B. Example

AB
1NameValue
2Adam100
3John0
4Nick50
5Tess75

<tbody>
</tbody>

What I want to do is the following. If the value in column B is 0, I want to hide the sheet with the name in column A. Example: If B3 is 0, hide sheet John. However, in the VBA I want to have A3 instead of John since the value can change.

The following would work, but the names change often so I would need a reference instead of hard copying the names.

------- VBA Example -------

Private Sub Worksheet_Change(ByVal Target As Range)

If [B2] = "0" Then
Sheets("Adam").Visible = False
Else
Sheets("Adam").Visible = True
End If


If [B3] = "0" Then
Sheets("John").Visible = False
Else
Sheets("John").Visible = True
End If

If [B4] = "0" Then
Sheets("Nick").Visible = False
Else
Sheets("Nick").Visible = True
End If




If [B5] = "0" Then
Sheets("Tess").Visible = False
Else
Sheets("Tess").Visible = True
End If



End Sub

------- End VBA Example -------

How can I replace Adam with A2, John with A3, etc.?

Thanks in advance for your support and let me know if you need further clarification.

Regards,
Ricky
 
Just change this line of code :
Code:
Private Sub Worksheet_Calculate()
to this
Code:
Sub Hide-UnhideRows
. Put the macro in a standard module and run it manually from there.
 
Upvote 0

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)

Forum statistics

Threads
1,214,994
Messages
6,122,633
Members
449,092
Latest member
bsb1122

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