Get Worksheet Name from CodeName

sparky2205

Well-known Member
Joined
Feb 6, 2013
Messages
507
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi folks,
I have the following macro which allows the copying of worksheets on a protected workbook.
It works fine but I was trying to replace:
Code:
sInterimName = ActiveSheet.Name
with something like
Code:
sc = Worksheets.Count
sInterimName = "Sheet" & sc.Name
In essence I want to retrieve the worksheet name using the codename.
Is this something straightforward?

Code:
Sub CopySheet()
Dim sName As String
Dim sInterimName As String
Dim iCountName As Integer
Dim ws As Worksheet
Dim pw As String
Dim sc As Integer
    pw = ""
    ScreenUpdating = False
    iCountName = 0
    sName = InputBox("Enter the worksheet name to copy")
    
    If sName = "" Then
        Exit Sub
    End If
    
    For Each ws In ThisWorkbook.Worksheets
        If UCase(ws.Name) = UCase(sName) Then
            iCountName = iCountName + 1
        End If
    Next ws
    
    If iCountName = 0 Then
        MsgBox "The selected worksheet name does not exist in this file"
        Exit Sub
    End If
    
    ThisWorkbook.Unprotect Password:=pw
    Sheets(sName).Copy after:=Sheets(sName)
    sInterimName = ActiveSheet.Name
    
    ThisWorkbook.Protect Password:=pw, structure:=True
                   
    Worksheets(sInterimName).Protect Password:=pw, _
        userinterfaceonly:=True, _
        AllowFiltering:=True, _
        AllowFormattingCells:=True, _
        AllowFormattingRows:=True, _
        AllowFormattingColumns:=True, _
        AllowInsertingRows:=True
        
        ScreenUpdating = True
        
End Sub
 
Last edited:

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
I'm not sure why you need the codename in this situation either, but you can replace this portion of your code
Code:
    For Each ws In ThisWorkbook.Worksheets
        If UCase(ws.Name) = UCase(sName) Then
            iCountName = iCountName + 1
        End If
    Next ws
    
    If iCountName = 0 Then
        MsgBox "The selected worksheet name does not exist in this file"
        Exit Sub
    End If
with
Code:
    If Not Evaluate("isref('" & sname & "'1A1)") Then
        MsgBox "The selected worksheet name does not exist in this file"
        Exit Sub
    End If
 
Upvote 0
Knowledge, gentlemen.
I was initially taking the target sheet name and adding " (2)" to it.
That was fine as long as the name of the newly copied sheet didn't end up exceeding 31 characters with the addition of " (2)".
This, of course, would get abbreviated and my sInterimName would then be incorrect.
So I changed to ActiveSheet.Name.
As I said the code works. I am just trying to specify the worksheet by name in case I should need that in the future.
I thought the best way to do that would be to use the codename as the copied sheet will always be the last sheet added so will have the max codename.
If there is another way to get the sheet name by all means please enlighten me. I am here to learn.

Thanks for the new piece of code to check if the worksheet name exists Fluff.
I imagined there had to be a neater way of doing this but what I had worked so I stuck with it. (Ironic I suppose, considering this post).
I'll have a look and see if I can figure out exactly what is happening in it. Some new stuff there I haven't seen before.
 
Upvote 0
You already have the name! After your code has hit this line in your macro:
Code:
sInterimName = ActiveSheet.Name
 
Upvote 0
Yes, I understand that, but what I was trying to do was replace that line of code with another that would get me the worksheet name by specifying the sheet rather than just using ActiveSheet.
That's why I was using the codename. The codename doesn't change.
And as the copied sheet will be the last sheet added to the workbook the numerical part of the codename e.g. Sheet22 will always be the max in the worksheet.
So, I was using Worksheet.Count to get that numerical value and append it to "Sheet" to get the codename.
Then the idea was to use that codename to retrieve the name of the worksheet.
That's when I ran into problems and contacted you guys.
 
Upvote 0
You cannot get the codename by concatenating values together like. Also the codename acts the same as a normal sheet name if sheets are deleted or renamed.
So if you deleted codename Sheet5 and at a later date added a new sheet it would be Sheet5 not Sheet23.
 
Upvote 0
So Excel fills in the blanks, so to speak, when creating new worksheets. I didn't realise that.
So, ActiveSheet it is then.
Thanks for your help folks.
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0
When you copy a worksheet it always becomes the activesheet, so if you catch the name (or set an object reference) of the activesheet just after the copy, you're good to go.
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

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