VBA not working when sheet locked

Russk68

Active Member
Joined
May 1, 2006
Messages
473
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
 

Some videos you may like

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,593
Office Version
  1. 2007
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

Active Member
Joined
May 1, 2006
Messages
473
Hey Dante
This is actually your original code.
Thanks for your help again!
Works great!
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,593
Office Version
  1. 2007
Platform
  1. Windows
It's pleasure and thanks for the feedback
 

Watch MrExcel Video

Forum statistics

Threads
1,109,359
Messages
5,528,225
Members
409,809
Latest member
VICKRAM

This Week's Hot Topics

  • Change military grades into rank
    Afternoon all Need help with formula that will change military rank (i.e. 1, 2, 3 into Amn, A1C, SrA). Running IF formula that does not work...
  • VBA COUNTIF SOLUTION
    Hi The following are the errors spread across the several columns from E to Q ie. 13 columns across several sheets with more than 500 rows per...
  • INSERT ROW WITH SPECIFIS TEXT IN A COLUMN
    Hi All! How can identify that that the row to be inserted has to be inserted before 1st row with specific text in column F. If I record the...
  • Auto-Create a monthly Sign in sheet for preschool students
    The image below is what each page looks like. Above is space for the "Child Name" "Month" "Class" School days are obviously Monday-Friday but...
  • VBA vlookup multiple results
    Hi folks, Hopefully someone out there can help. I have a list to vlookup which works (ish). the lookup only picks up the first instance of the...
  • Extract values for earliest/latest times
    I am trying to put together a formula to get the earliest start time, the latest end time from column A for each person in Column B-F without the...
Top