Get Worksheet Name from CodeName

sparky2205

Active Member
Joined
Feb 6, 2013
Messages
480
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:
Hi Fluff, me again,
can you explain your earlier code to me please?
I'm struggling with it, especially how you can check for the existence of the Sheet Name in one action.
I haven't used either the Evaluate or isref functions before.

Thanks.
 
Upvote 0

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Hi Fluff,
I now understand what IsRef and Evaluate do.
What I don't understand is how IsRef checks all worksheets without a loop.
Code:
If Not Evaluate("isref('" & sname & "'!A1)") Then
        MsgBox "The selected worksheet name does not exist in this file"
        Exit Sub
End If
Does IsRef automatically check all worksheets in the workbook when used in vba code?
I have seen examples where it is used in a workbook cell but only to refer to one worksheet at a time.
Or is it Evaluate that forces this?
I can't see this explained anywhere.
 
Upvote 0
It doesn't check all worksheets, it just checks to see if it's a valid reference (ie does the sheet exist)
 
Upvote 0

Forum statistics

Threads
1,215,200
Messages
6,123,611
Members
449,109
Latest member
Sebas8956

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