VBA Hide/Unhide Worksheets - Runtime Error

delaney1102

New Member
Joined
Aug 14, 2019
Messages
14
Hi all,
I have a workbook that's combining time off calendars for a couple departments with hyperlinks to hidden worksheets (months) for each department.

This is what I have written on the Navigation tab

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
LinkTo = Target.SubAddress
WhereBang = InStr(1, LinkTo, "!")
If WhereBang > 0 Then
MySheet = Left(LinkTo, WhereBang - 1)
Worksheets(MySheet).Visible = True
Worksheets(MySheet).Select
MyAddr = Mid(LinkTo, WhereBang + 1)
Worksheets(MySheet).Range(MyAddr).Select
End If


End Sub


The macro works great for the first group of months, but when I click on say June/July/August or whatever in the second group of months, I get a Runtime Error 9 message and the line that is highlighted when I debug is:

Worksheets(MySheet).Visible = True


The second group of months are named "June (2)", "July (2)", etc. I thought that could that be my problem but when I tried renaming the second group of months but then nothing worked, not even the first group of hyperlinks.

Any help would be appreciated!!
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Hi & welcome to MrExcel.
Try
Code:
MySheet = Replace(Left(LinkTo, WhereBang - 1), "'", "")
 
Upvote 0
Solution
When you get the error have you checked that MySheet contains a value that's a valid sheet name in the active workbook?
 
Upvote 0
Just replace this line
Code:
MySheet = Left(LinkTo, WhereBang - 1)
with my suggestion.
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,650
Messages
6,120,736
Members
448,988
Latest member
BB_Unlv

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