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.
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Can I see your current code and a sample of the workbook? Could you possibly upload the workbook using the XL2BB function?
 
Upvote 0
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.
 
Upvote 0
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"
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,214,807
Messages
6,121,679
Members
449,047
Latest member
notmrdurden

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