VBA macros not working with protected sheet

Corey_Riley

New Member
Joined
Sep 6, 2022
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hi Everyone,

I'm new to VBA but I was able to create the following code that allows me to click a hyperlinked button which takes me to a hidden worksheet in my workbook and hides the original worksheet.

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)

linkto = Target.SubAddress
wherebang = InStr(1, linkto, "!")
If wherebang > 0 Then
mysheet = Replace(Left(linkto, wherebang - 1), "'", "")

Worksheets(mysheet).Visible = True

Worksheets(mysheet).Select
myaddr = Mid(linkto, wherebang + 1)
Worksheets("Introduction").Visible = False

Worksheets(mysheet).Range(myaddr).Select

End If

End Sub

I want to share my document with my team but I need to protect my document so they can't mess up the coding or structure of the document.

When ever I protect my document an click on one of the buttons, the following message comes up:

1662472980137.png


I'm not sure what to do. Please help. (There is no password set for the protection function)
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Hi there and welcome to the forum... which sheet the one that needs to be protected?

Could maybe try...

VBA Code:
Option Explicit
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
    linkto = Target.SubAddress
    wherebang = InStr(1, linkto, "!")
    If wherebang > 0 Then
        mysheet = Replace(Left(linkto, wherebang - 1), "'", "")
        Worksheets(mysheet).Unprotect
        Worksheets(mysheet).Visible = True
        Worksheets(mysheet).Select
        myaddr = Mid(linkto, wherebang + 1)
        Worksheets("Introduction").Visible = False
        Worksheets(mysheet).Range(myaddr).Select
    End If
    Worksheets(mysheet).Protect
End Sub
 
Upvote 0
You will need to declare linkto as a Variable... Or just try to remove option explicit from the top of code...

Only back at my PC tomorrow unfortunately...
 
Upvote 0
I've found a way around it.

I just protect each sheet individually and select all of the tick boxes.

I'm still not sure why the macro wouldn't work though
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,157
Members
453,021
Latest member
Justyna P

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