View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
ker_01 ker_01 is offline
external usenet poster
 
Posts: 395
Default for loop with If then question


Steve-

If you are only checking to see if a specific value is /present/ to lock the
row, there isn't much harm in letting it check the rows prior to your data-
it won't find the value and won't lock the row(s). That is a simpler solution
than coding for the start row for every sheet; it may take a few more
miliseconds to process, but you won't notice.

Here is some code to find the last used row- my apologies to whomever
originally posted this, I didn't keep that noted in the workbook I just
pulled it from.

Function lastRow(sh As Worksheet) 'Finds last cell with data in the last
used row.
On Error Resume Next
lastRow = sh.Cells.Find(What:="*", After:=sh.Range("A1"), _
LookAt:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, MatchCase:=False).Row
On Error GoTo 0
End Function

That should give you a better number for lastrow on each sheet

So overall, something like (aircode)

Dim sht as worksheet

For each sht in Activeworkbook.sheets
ThisSheetsLastRow = lastrow(sht)
For i = 1 to ThisSheetsLastRow
'your code to check each row and lock or unlock it
Next
Next

"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.