Private Sub Worksheet_SelectionChange

boxsterman

Active Member
Joined
Apr 16, 2002
Messages
285
Hi all,

I have some code on a particular worksheet. All is fine when the sheet is not protected, however when protected I receive a Run-time error '1004': Unable to set the Hidden porperty of the Range class.

Here is some of the code, it errors out on the hidden "True" line.


Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Not IsNumeric(Range("C68")) Then Exit Sub
If Range("C68").Value > 0.03 Then
Range("73:85").EntireRow.Hidden = False
Else
Range("73:85").EntireRow.Hidden = True
End If

Thank you very much.
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Try something like this.

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
ActiveSheet.Unprotect Password:="password here"
If Not IsNumeric(Range("C68")) Then Exit Sub
If Range("C68").Value > 0.03 Then
Range("73:85").EntireRow.Hidden = False
Else
Range("73:85").EntireRow.Hidden = True
ActiveSheet.protect Password:="password here"
End If
 
Upvote 0
Hi boxterman,

either unprotect the sheet in question right at the start of the event and protect it again at the end but using Worksheet_SelectionChange makes it more useful to use the volatile setting of UserInterfaceOnly as additional parameter when using Protect. This could be done in Workbook_Open or Worksheet_Activate and would allow macros to run without problems. Volatile means that this status will not be saved and needs to be refreshed every time the workbook is opened again.

Sample code may look like
VBA Code:
Private Sub Worksheet_Activate()
  With ActiveSheet
    .EnableSelection = xlNoSelection
    .Protect Contents:=True, UserInterfaceOnly:=True
  End With
End Sub

This would allow code to run on worksheet with code behind it.

Holger
 
Upvote 0
Hi boxtserman,

marking your own post without a hint on how you have done it is not the solution to me...

Holger
 
Upvote 0
Thanks everyone. I believe I got it to work,

Good to hear you got the solution.
If you would like to post the solution then it is perfectly fine to mark your post as the solution to help future readers. Otherwise, please do not mark a post that doesn't contain a solution.
 
Upvote 0

Forum statistics

Threads
1,215,440
Messages
6,124,879
Members
449,192
Latest member
MoonDancer

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