Home |
Search |
Today's Posts |
#11
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Yup. This did it. Thank you.
As all of elements that I'll be looking for in this routine will be 5 characters, the trim should do it. "Ryan H" wrote: To use the OR operator in VBA you have to have two expressions on each side of the operator. See the help section on OR in the VBE. So your line of code would have to look like this: If .Cells(rw, "A").Value < "" Or .Cells(rw, "A").Value < " " Then But you really should do it this way. You really should use: If Trim(.Cells(rw, "A").Value) < "" Then TRIM will automatically remove all spaces before and after a string, but will not remove spaces inside a string. You can see the help section on the TRIM function as well Hope this helps! If so, let me know, click "YES" below. -- Cheers, Ryan "Steve" wrote: Hi Ryan, I've made a modification to your code as follows. If .Cells(rw, "A").value < "" Or " " Then It's calling a type mismatch error. I haven't changed anything else to your code. I don't see anything in the excel help file that tells me I cannot use OR in this manner. Thank you. "Ryan H" wrote: This code will loop through all your worksheets. I assume the data you are trying to find is in Col. A. With each worksheet it will unlock all cells then lock the row that contains the value you specify. You will have to change your password and data value in this code to fit your application. Hope this helps! If so, let me know, click "YES" below. Sub LockRows() Dim wks As Worksheet Dim LastRow As Long Dim rw As Long For Each wks In Worksheets With wks .Unprotect Password:="Your Password Here" .Cells.Locked = False LastRow = .Cells(Rows.Count, "A").End(xlUp).Row For rw = 2 To LastRow If .Cells(rw, "A").Value = "Your Data" Then .Rows(rw).Locked = True End If Next rw .Protect Password:="Your Password Here" End With Next wks End Sub -- Cheers, Ryan "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. |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Loop Do Until question | Excel Programming | |||
For Next Loop Question | Excel Programming | |||
end with loop question | Excel Programming | |||
do until...loop question | Excel Programming | |||
another loop question | Excel Programming |