Rename the codename of the sheet and use that name in the remaining code

praveenpatel421983

New Member
Joined
Aug 17, 2017
Messages
41
Hi all,

This forum has always helped me when I get stuck with excel vba. I need help again.

I am trying to copy a worksheet named "Work" as "Job" in same workbook and trying to protect "Job" sheet. I came up to following code and I am stuck. Please help.


Code:
Sub RenameSht() 
Dim ws As Worksheet

ThisWorkbook.Sheets("Work").Copy After:=ThisWorkbook.Sheets(Sheets.Count)
ActiveSheet.Name = "Job"

For Each ws In ThisWorkbook.Worksheets
   If ws.Name = "Job" Then
      x = ws.CodeName
      y = "Rename1"
      ThisWorkbook.VBProject.VBComponents(x).Name = y
   End If
Next ws
    
If y.ProtectContents = False Then
   y.Protect "test"
    End If

End Sub

Thanks
Praveen
 
Last edited by a moderator:

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Try...

Code:
Sub RenameSht()

    Dim ws As Worksheet
    
    ThisWorkbook.Sheets("Work").Copy After:=ThisWorkbook.Sheets(Sheets.Count)
    
    With ActiveSheet
        .Name = "Job"
        .Protect "test"
        ThisWorkbook.VBProject.VBComponents(Sheets(.Name).CodeName).Name = "Rename1"
    End With


End Sub

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,322
Members
452,635
Latest member
laura12345

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