Opening number of sheets according to cell value

ahid13

New Member
Joined
Aug 29, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi! I'm new to VBA and trying to figure out how to simplify the codes I have.
Below is one I have where user will select a value from cover sheet (1,2,3,etc.) from cell [no_pin] and the number selected will determine the sheets that will be visible ("Pin-1","Pin-2", etc.).
The title of the worksheet is essentially "Pin-(cellvalue)".

Sub Macro2(ByVal Target As Range)

Dim pn1 As Worksheet
Set pn1 = Sheets("Pin-1")
Dim pn2 As Worksheet
Set pn2 = Sheets("Pin-2")
Dim pn3 As Worksheet
Set pn3 = Sheets("Pin-3")

If [no_pin] = "0" Then
pn1.Visible = xlSheetHidden
pn2.Visible = xlSheetHidden
pn3.Visible = xlSheetHidden

Else
pn1.Visible = xlSheetVisible
pn2.Visible = xlSheetVisible
pn3.Visible = xlSheetVisible
End If

If [no_axle] = "1" Then
ax2.Visible = xlSheetHidden
ax3.Visible = xlSheetHidden
End If

If [no_axle] = "2" Then
ax3.Visible = xlSheetHidden
End If


End Sub


Thanks in advance!
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hi! I'm new to VBA and trying to figure out how to simplify the codes I have.
Below is one I have where user will select a value from cover sheet (1,2,3,etc.) from cell [no_pin] and the number selected will determine the sheets that will be visible ("Pin-1","Pin-2", etc.).
The title of the worksheet is essentially "Pin-(cellvalue)".

Sub Macro2(ByVal Target As Range)

Dim pn1 As Worksheet
Set pn1 = Sheets("Pin-1")
Dim pn2 As Worksheet
Set pn2 = Sheets("Pin-2")
Dim pn3 As Worksheet
Set pn3 = Sheets("Pin-3")

If [no_pin] = "0" Then
pn1.Visible = xlSheetHidden
pn2.Visible = xlSheetHidden
pn3.Visible = xlSheetHidden

Else
pn1.Visible = xlSheetVisible
pn2.Visible = xlSheetVisible
pn3.Visible = xlSheetVisible
End If

If [no_axle] = "1" Then
ax2.Visible = xlSheetHidden
ax3.Visible = xlSheetHidden
End If

If [no_axle] = "2" Then
ax3.Visible = xlSheetHidden
End If


End Sub


Thanks in advance!
Found an answer from another post!
 
Upvote 0

Forum statistics

Threads
1,215,415
Messages
6,124,768
Members
449,187
Latest member
hermansoa

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