Protecting via Macro

  • Thread starter Thread starter Random NumNuts
  • Start date Start date
R

Random NumNuts

I have a sheet that is protected. I would like to unprotect it, hide some
rows, and re-protect it under macro control. I realize this may expose the
password, but, let's face it, security is tentative at best.
Thanks../Randy
 
Hi
something like
sub foo()
with activesheet
.unprotect password:="yourpassword"
.rows(1).hidden=true
.protect password:="yourpassword"
end with
end sub
 
Ooops... I'm still having troulbe. Here is what I entered:

Sub NEWVIEW()
'
' HideYellow Macro
' Macro recorded 5-26-2004 by BTudor
'

Dim RowNdx As Long
Dim LastRow As Long
With ActiveSheet.Unprotect.password = "GP159"

LastRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
For RowNdx = LastRow To 1 Step -1
If (Cells(RowNdx, "A").Interior.ColorIndex = 36) Or (Cells(RowNdx,
"A").Interior.ColorIndex = 35) Then
Rows(RowNdx).Hidden = True
End If
Next RowNdx
With ActiveSheet.Protect.password = "GP159"
End With

End Sub



I get: Compiule Error: Expected End With Any thoughts? (I noticed you
had := -- that didn't seem to work either).
 
This uses stuff closer to what Frank posted.

Option Explicit
Sub NEWVIEW()
'
' HideYellow Macro
' Macro recorded 5-26-2004 by BTudor
'

Dim RowNdx As Long
Dim LastRow As Long

With ActiveSheet
.Unprotect Password:="GP159"
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
For RowNdx = LastRow To 1 Step -1
If (.Cells(RowNdx, "A").Interior.ColorIndex = 36) _
Or (.Cells(RowNdx, "A").Interior.ColorIndex = 35) Then
.Rows(RowNdx).Hidden = True
End If
Next RowNdx
.Protect Password:="GP159"
End With

End Sub
 
It's just Dave's way of swearing <g>

It will force explicit declaration of all variables in the module,
if you use an undeclared variable name you will get an error.
I guess you could call it a "spell checker" of variables <g>

--
For everyone's benefit keep the discussion in the newsgroup.

Regards,

Peo Sjoblom
 
If Option Explicit is at the top of the worksheet, you have to declare
each variable that you use in the module. For example, if your macro is:

Sub MyCount()
For i = 1 To 3
MsgBox i
Next i
End Sub

when you try to run it you'll get a compile error: variable not defined.

Include a declaration line at the top of the macro, and it will run
correctly:

Sub MyCount()
Dim i As Integer
For i = 1 To 5
MsgBox i
Next i
End Sub

You should turn this feature on, so the Option Explicit line appears at
the top of all new worksheets --

In Excel, press Alt+F11 to open the Visual Basic Explorer
Choose Tools>Options
 
Back
Top