View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Mike H Mike H is offline
external usenet poster
 
Posts: 11,501
Default for loop with If then question

Steve,

I wasn't clear what the protection criteria is so this loops down column A
firstrow to last row and locks cell if it finds 'This" or "That"

Sub Lock_Em_Up()
Dim FirstRow As Long
Dim LastRow As Long

Set sht = Sheets("Sheet1")
sht.Unprotect Password:="MyPass"
sht.Cells.Locked = False

FirstRow = sht.Range("A1").End(xlDown).Row
LastRow = sht.Cells(Rows.Count, "A").End(xlUp).Row

For x = FirstRow To LastRow
If sht.Cells(x, 1).Value = "This" Or sht.Cells(x, 1).Value = "That" Then
sht.Rows(x).Locked = True
End If
Next
sht.Protect Password:="MyPass"
End Sub
--
Mike

When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.


"Steve" wrote:

Morning all.
I'm looking to set up an automatic password sheet protection tool and there
are only specific elements that I need protected on the worksheets in my
workbooks.
As such, my goal is to look for a value, in a single column, and if the
value exists in one cell, protect that row. All other rows will remain
unprotected.

As I think about this, it seems to me that I'd need a for loop to iterate
through the rows of that one column, and then use an IF statement to look for
a value.

Part of my struggle is that not all worksheets start at the same start row.
Nor do all worksheets end at the same row. Therefore, I need a variable to
delineate my start and end points. I've already learned that LastUsedRow does
not work well enough to use for this.

E.g.
for i = firstusedrow to lastusedrow step 1
if .cell() < "" OR " " then
Activesheet.protect.........

Your helps are appreciated.