Dynamically access a worksheet by its sheet number / index NOT its name in VBA

awecomms

New Member
Joined
Jan 20, 2019
Messages
5
I am trying to parse a simple statement but cant seem to work it out.
I have a list of stakeholders, upto 10. I enter their names into a list and I need VBA to rename a hidden sheet to that Stakeholder's name then unhide it. I have 10 hidden sheets, labelled "Stakeholder1" ... "Stakeholder10, with sheet indexes of Sheet11 to Sheet20. Because each one is tied to a cell I want to "link" the VBA to reference Sheet11 when "Stakeholder1"'s cell is edited. I can do it by sheet name but I have to track the name and if some butthead enters the name "Stakeholder3" in the cell for Stakeholder 5, everything goes to heck in a wicker basket.

My problem to date with searching for answers is I either can't explain it properly in my search text or Google is "helping" by "understanding" what I was "really" looking for.
 

Some videos you may like

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.

rhratterman15

New Member
Joined
May 11, 2020
Messages
16
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2013
Platform
  1. Windows
  2. Mobile
  3. Web
Can I see your current code and a sample of the workbook? Could you possibly upload the workbook using the XL2BB function?
 

awecomms

New Member
Joined
Jan 20, 2019
Messages
5
I am trying to parse a simple statement but cant seem to work it out.
I have a list of stakeholders, upto 10. I enter their names into a list and I need VBA to rename a hidden sheet to that Stakeholder's name then unhide it. I have 10 hidden sheets, labelled "Stakeholder1" ... "Stakeholder10, with sheet indexes of Sheet11 to Sheet20. Because each one is tied to a cell I want to "link" the VBA to reference Sheet11 when "Stakeholder1"'s cell is edited. I can do it by sheet name but I have to track the name and if some butthead enters the name "Stakeholder3" in the cell for Stakeholder 5, everything goes to heck in a wicker basket.

My problem to date with searching for answers is I either can't explain it properly in my search text or Google is "helping" by "understanding" what I was "really" looking for.

Turns out, I am wanting to access a worksheet by codename, something I am getting the feeling is not possible in the simplistic way I was hoping.
I was trying something like sh = Sheet(sheetnum) where sheetnum is the numeric component of the worksheet's codename.
 

StephenCrump

MrExcel MVP
Joined
Sep 18, 2013
Messages
4,121
Office Version
  1. 365
Platform
  1. Windows
Turns out, I am wanting to access a worksheet by codename, something I am getting the feeling is not possible in the simplistic way I was hoping.
I was trying something like sh = Sheet(sheetnum) where sheetnum is the numeric component of the worksheet's codename.

Correct, it's not as easy as referring to sheet name, where you can, for example:

Code:
Set sh = Worksheets("Sheet" & i)

But there are workarounds, e.g. assuming codenames Sheet11, Sheet12 ... Sheet20, you can, for example:

VBA Code:
Dim v As Variant
Dim i As Long

v = Array(Sheet11, Sheet12, Sheet13, Sheet14, Sheet15, Sheet16, Sheet17, Sheet18, Sheet19, Sheet20)

i = 3
v(i).Range("A1").Value = "Hello"
 

awecomms

New Member
Joined
Jan 20, 2019
Messages
5
ended up getting ugly with it

For Each sh In ThisWorkbook.Worksheets
If sh.CodeName = cursht Then
With sh
.Name = sheetname
.Visible = True
End With
End If
If Left(sh.Name, 11) = "Stakeholder" Then
If Left(sh.Name, 12) <> "Stakeholders" Then
sh.Visible = False
End If
End If
Next sh

But I LOVE the array idea as I know the stakeholders number and codenames never change

Genius!

This would also work with non contiguous sheets too.

Off to simplify some code.
 

Watch MrExcel Video

Forum statistics

Threads
1,128,107
Messages
5,628,731
Members
416,333
Latest member
Time2Learn

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
Top