VBA not working when sheet locked

Russk68

Well-known Member
Joined
May 1, 2006
Messages
543
Office Version
  1. 365
Platform
  1. MacOS
Hi All,

The (Incomplete) code below hides rows based on a value of GF3 and works great until I password protect the sheet. How can I get this to work when the sheet is PW protected? I assume the code would have to unlock/lock the sheet each time the macro is ran?


If Target.Count = 1 Then
If Range("GF3").Value < "3" Then
Rows("13:52").EntireRow.Hidden = True
ElseIf Range("GF3").Value = "6" Then
Rows("13:52").EntireRow.Hidden = False
End If
End If
End If
If Not Intersect(Target, Range("F1")) Is Nothing Then 'Distro#1x24
If Target.Count = 1 Then
If Range("GF3").Value < "4" Then
Rows("21:52").EntireRow.Hidden = True
End If
End If
End If
If Not Intersect(Target, Range("F1")) Is Nothing Then 'Distro#1x48
If Target.Count = 1 Then
If Range("GF3").Value < "5" Then
Rows("37:52").EntireRow.Hidden = True
End If End If
End If
If Not Intersect(Target, Range("F1")) Is Nothing Then 'Distro#1x60
If Target.Count = 1 Then
If Range("GF3").Value < "6" Then
Rows("45:52").EntireRow.Hidden = True
End If
End If
End If

Thank you!
Russ
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
14,796
Office Version
  1. 2010
Platform
  1. Windows
Try this. Change "abc" by the desired password


Code:
[COLOR=#0000ff]activesheet.unprotect "abc"[/COLOR]

[COLOR=#333333]If Target.Count = 1 Then[/COLOR]
[COLOR=#333333]If Range("GF3").Value < "3" Then[/COLOR]
[COLOR=#333333]Rows("13:52").EntireRow.Hidden = True[/COLOR]
[COLOR=#333333]ElseIf Range("GF3").Value = "6" Then[/COLOR]
[COLOR=#333333]Rows("13:52").EntireRow.Hidden = False[/COLOR]
[COLOR=#333333]End If[/COLOR]
[COLOR=#333333]End If[/COLOR]
[COLOR=#333333]End If[/COLOR]
[COLOR=#333333]If Not Intersect(Target, Range("F1")) Is Nothing Then 'Distro#1x24[/COLOR]
[COLOR=#333333]If Target.Count = 1 Then[/COLOR]
[COLOR=#333333]If Range("GF3").Value < "4" Then[/COLOR]
[COLOR=#333333]Rows("21:52").EntireRow.Hidden = True[/COLOR]
[COLOR=#333333]End If[/COLOR]
[COLOR=#333333]End If[/COLOR]
[COLOR=#333333]End If[/COLOR]
[COLOR=#333333]If Not Intersect(Target, Range("F1")) Is Nothing Then 'Distro#1x48[/COLOR]
[COLOR=#333333]If Target.Count = 1 Then[/COLOR]
[COLOR=#333333]If Range("GF3").Value < "5" Then[/COLOR]
[COLOR=#333333]Rows("37:52").EntireRow.Hidden = True[/COLOR]
[COLOR=#333333]End If End If[/COLOR]
[COLOR=#333333]End If[/COLOR]
[COLOR=#333333]If Not Intersect(Target, Range("F1")) Is Nothing Then 'Distro#1x60[/COLOR]
[COLOR=#333333]If Target.Count = 1 Then[/COLOR]
[COLOR=#333333]If Range("GF3").Value < "6" Then[/COLOR]
[COLOR=#333333]Rows("45:52").EntireRow.Hidden = True[/COLOR]
[COLOR=#333333]End If[/COLOR]
[COLOR=#333333]End If[/COLOR]
[COLOR=#333333]End If

[/COLOR][COLOR=#0000ff]activesheet.protect "abc"[/COLOR]
 

Russk68

Well-known Member
Joined
May 1, 2006
Messages
543
Office Version
  1. 365
Platform
  1. MacOS
Hey Dante
This is actually your original code.
Thanks for your help again!
Works great!
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
14,796
Office Version
  1. 2010
Platform
  1. Windows
It's pleasure and thanks for the feedback
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,297
Messages
5,836,481
Members
430,434
Latest member
whatabout

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
Top