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

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
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
Thanks Domenic for the reply. This is what I was missing. I will check this code, if I face any issue I will be back with more questions :)
 
Upvote 0

Forum statistics

Threads
1,215,539
Messages
6,125,403
Members
449,222
Latest member
taner zz

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