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

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
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,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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