Get sheet name from composite code name

Giordano Bruno

Well-known Member
Joined
Jan 7, 2007
Messages
1,345
Is there a simple way of retrieving the worksheet name from the code name when the code name = "Sheet" & "1", "2", "3", etc. I can do it by looping through all worksheets, but there must be an easier way.
Thanks in advance for any suggestions.
 
Hi Stephen. That's more or less what I am doing except that there are nine worksheets being imported. I had hoped to find a simple one line solution, but it does not appear to be possible.

So you're going to be looping from 1 to 9 in any event to process each worksheet. If you know where the worksheets will be located, e.g always starting at worksheet number 3, and consecutive, you can loop from Worksheets(3) to Worksheets(11).

If they could be anywhere, you'll need to rely on an indirect method, e.g. testing whether worksheet names are different to your pre-existing names, or codenames different to your pre-existing codenames. Whichever way you go will just add a few quick iterations to the loop.
 
Last edited:
Upvote 0

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Hi Mike. Nice to hear from you. I had thought of doing that, but it does require the user to put the new worksheets at the end. I have absolutely no confidence that the user will always do that so the Index property will not be reliable. I'm trying to make it foolproof even though I know that the fools will always outwit me in the end.

Perhaps you should have a companion "Put in the right order" macro that you run as needed. A utility that takes your input.

Is there anything in the structure of the sheets (number of columns?, column headers?) that would identify a sheet by it's contents?
 
Last edited:
Upvote 0
I'm trying to make it foolproof even though I know that the fools will always outwit me in the end.

This can be proven mathematically: Given a workbook protected N ways against dumb user behaviour, there exist at least N+1 ways the user will find to break it.
 
Upvote 0
Hi Stephen and Mike. I was looking for a one line solution. If that's not available, I might as well stick to a simple loop through all sheets. It's easier to write and does not really take so long that it's noticeable.
I like you mathematical proof Stephen. I think it's called "Murphy's Law".
 
Upvote 0
One thing that I'm thinking is that if your loop only sets variables, then you will have to run that loop every time that VBA loses its values. But if you loop and re-position or re-tab-name the sheets, then you will have a clean workbook that can be processed in the future without looping.

The advantages of that vary depending on if you are doing a one-off type macro or if you are archiving stuff, but if you are archiving, it would be worth your wile to standardize every workbook that you get so that repeated sheet looping isn't needed.
 
Upvote 0
Hi Mike,
Always nice to hear from you. In this case, the code will be run only occasionally when a fresh load of data is available. Once the other worksheets are loaded, they are copied to the correct locations and the field headers are checked for consistency and then the new worksheets are deleted.
Many thanks for your contributions both here and in the past.
 
Upvote 0
If the names of the imported worksheets are not edited by the user and stay the same throughout then would this work for you ?

Code in the ThisWorkbook Module :

Code:
Private WithEvents wb As Workbook

Private Sub Workbook_Open()
    Set wb = Me
End Sub

Private Sub wb_NewSheet(ByVal sh As Object)

    On Error Resume Next
        Me.CustomDocumentProperties(sh.CodeName).Delete
    On Error GoTo 0
    Me.CustomDocumentProperties.Add Name:=sh.CodeName, _
    LinkToContent:=False, _
    Type:=msoPropertyTypeString, _
    Value:=sh.Name

End Sub

Private Function GetSheetNameFromCodeName(ByVal CodeName As String) As String
    GetSheetNameFromCodeName = Me.CustomDocumentProperties(CodeName)
End Function

Sub test()
    MsgBox GetSheetNameFromCodeName(CodeName:="Sheet1")
End Sub
 
Upvote 0
Using the _SheetActivate event instead of the _NewSheet would be better as follows :

Code:
Option Explicit

Private WithEvents wb As Workbook

Private Sub wb_SheetActivate(ByVal Sh As Object)
    On Error Resume Next
        Me.CustomDocumentProperties(Sh.CodeName).Delete
    On Error GoTo 0
    Me.CustomDocumentProperties.Add Name:=Sh.CodeName, _
    LinkToContent:=False, _
    Type:=msoPropertyTypeString, _
    Value:=Sh.Name
End Sub

Private Sub Workbook_Open()
    Set wb = Me
End Sub

Private Function GetSheetNameFromCodeName(ByVal CodeName As String) As String
    GetSheetNameFromCodeName = Me.CustomDocumentProperties(CodeName)
End Function

Sub test()
    MsgBox GetSheetNameFromCodeName(CodeName:="Sheet1")
End Sub
 
Last edited:
Upvote 0
Hi Jaafar. Looks like black magic to me so I can't work out why when I run the "test" sub I get a Run-time error 5 on the "GetSheetNameFromCodeName = ..." line. All of the code is in the "ThisWorkbook" module and my workbook has worksheets Sheet1 thru Sheet9. Do you have any idea what I might be doing wrong?
 
Upvote 0
Hi Jaafar. Looks like black magic to me so I can't work out why when I run the "test" sub I get a Run-time error 5 on the "GetSheetNameFromCodeName = ..." line. All of the code is in the "ThisWorkbook" module and my workbook has worksheets Sheet1 thru Sheet9. Do you have any idea what I might be doing wrong?

The code is meant to take effect when the workbook is re-opened (ie: the Workbook_Open event has to first be executed) and before the worksheets are imported.


Edit:
In other words:

1- run the Workbook_Open event
2- Import the worksheets
3- run the Test Macro
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,028
Messages
6,128,392
Members
449,445
Latest member
JJFabEngineering

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