Locking Tabs within a Workbook that has macros - Novice VBA

Zaytch

New Member
Joined
Sep 14, 2016
Messages
1
Hi there! I am trying to lock tabs in a workbook that has links to other spreadsheets and macros. I found help at the following link:

How to prevent users changing the Worksheet Name with VBA | Dedicated Excel

However when I use the code given:

Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
If ActiveSheet.Name <> "Data" Then
ActiveSheet.Name = "Data"
End If
End Sub

I get the following error:

Compile error: Expected: Then or GoTo

I am too novice to know what this means or how to fix it. Can you 1) help me fix it, or give me another example of a code that will help me lock down my Spreadsheet names?

Thanks!
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
The above code works for me...

However I think I would prefer to either make it a Workbook_SheetSelectionChange event (that way it will reset the name even if you click on a different sheet...)

In the Workbook code area put...
Code:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
If Sheets([COLOR=#ff0000]1[/COLOR]).Name <> "Data" Then
    Sheets([COLOR=#ff0000]1[/COLOR]).Name = "Data"
End If
End Sub
where 1 is the number of the sheet you would like to keep as "Data" (you can see what number the sheet is for yourself in the project browser.)


...or just password protect your workbook similarly to how you'd password protect a sheet.
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,733
Members
448,987
Latest member
marion_davis

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