Results 1 to 7 of 7

Thread: [RESOLVED] Scan for Locked Cells

  1. #1

    Thread Starter
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Resolved [RESOLVED] Scan for Locked Cells

    I have code that scans for locked cells.. only problem is it says they are ALL locked.

    I have range(A1:C13) as not locked.. and the worksheet is protected.
    Now I can change values in that range but no where else...
    but code returns ALL cells as locked??

    whats wrong with this:
    VB Code:
    1. Sub ScanCell()
    2. Dim WB As Workbook
    3. Dim WS As Worksheet
    4. Dim tWS As Worksheet
    5. Set WB = ActiveWorkbook
    6. Dim sRange_Col As String
    7. Dim sRange_Row As Long
    8. Dim tmp As String
    9. Dim tmpR() As String
    10. Dim lRow As Integer
    11. Dim lCol As Integer
    12. lRow = 2
    13. lCol = 64
    14. WB.Worksheets.Add Sheets(1)
    15. Set tWS = ActiveSheet
    16. tWS.Name = "LOCKED CELLS"
    17. For Each WS In WB.Worksheets
    18.     If WS.Name <> tWS.Name Then
    19.         tWS.Range("A1") = "LOCKED CELLS"
    20.         lRow = 2
    21.         lCol = lCol + 1
    22.         tmp = Replace(WS.UsedRange.Address, "$A$1:", "")
    23.         tmpR = Split(tmp, "$")
    24.         sRange_Col = tmpR(1)
    25.         sRange_Row = tmpR(2)
    26.         For col = 65 To Asc(sRange_Col)
    27.             For Row = 1 To sRange_Row
    28.                 If Range(Chr(col) & Row).Locked Then
    29.                      tWS.Range(Chr(lCol) & lRow) = WS.Name & "!" & Chr(col) & Row
    30.                      lRow = lRow + 1
    31.                 End If
    32.             Next
    33.         Next
    34.     End If
    35. Next
    36. End Sub

    Thanks!!
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  2. #2
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Scan for Locked Cells

    If your row number is greater than 9, then this line of code will alyways give you a row number of 1 , so you're not really checking your whole range

    E.g Cell C$14; tmpR = C14; sRange_Row = 1


    VB Code:
    1. sRange_Row = tmpR(2)
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  3. #3

    Thread Starter
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Scan for Locked Cells

    sorry, but you are incorrect

    VB Code:
    1. tmp = Replace(WS.UsedRange.Address, "$A$1:", "") 'Used Range is $A$1:$K$23
    2. 'tmp now = "$K$23"
    3.         tmpR = Split(tmp, "$") 'Splits by $
    4.         sRange_Col = tmpR(1) 'Now = K
    5.         sRange_Row = tmpR(2)'Now = 23

    the output also shows the entire range.. if I was not scanning the whole sheet.. the output would not show the entire sheet
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  4. #4
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Scan for Locked Cells

    Doh - it is an array, not a variable - my bad.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  5. #5

    Thread Starter
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Scan for Locked Cells

    no problem..
    You did make me catch something though.. I changed code now to scan ONLY the used range.. not A1 through Used Upper range...
    new code:
    VB Code:
    1. Sub ScanCell()
    2. Dim WB As Workbook
    3. Dim WS As Worksheet
    4. Dim tWS As Worksheet
    5. Set WB = ActiveWorkbook
    6. Dim sRange_Col1 As String
    7. Dim sRange_Row1 As Long
    8. Dim sRange_Col2 As String
    9. Dim sRange_Row2 As Long
    10. Dim tmp As String
    11. Dim tmpR1() As String
    12. Dim tmpR2() As String
    13. Dim lRow As Integer
    14. Dim lCol As Integer
    15. lRow = 2
    16. lCol = 64
    17. WB.Worksheets.Add Sheets(1)
    18. Set tWS = ActiveSheet
    19. tWS.Name = "LOCKED CELLS"
    20. For Each WS In WB.Worksheets
    21.     If WS.Name <> tWS.Name Then
    22.         tWS.Range("A1") = "LOCKED CELLS"
    23.         lRow = 2
    24.         lCol = lCol + 1
    25.         tmp = WS.UsedRange.Address
    26.         If InStr(tmp, ":") Then
    27.             tmpR1 = Split(tmp, ":")
    28.             tmpR2 = Split(tmpR1(1), "$")
    29.             sRange_Col2 = tmpR2(1)
    30.             sRange_Row2 = tmpR2(2)
    31.             tmpR2 = Split(tmpR1(0), "$")
    32.             sRange_Col1 = tmpR2(1)
    33.             sRange_Row1 = tmpR2(2)
    34.         Else
    35.             tmpR1 = Split(tmp, "$")
    36.             sRange_Col1 = tmpR1(1)
    37.             sRange_Row1 = tmpR1(2)
    38.             sRange_Col2 = tmpR1(1)
    39.             sRange_Row2 = tmpR1(2)
    40.         End If
    41.         For col = Asc(sRange_Col1) To Asc(sRange_Col2)
    42.             For Row = sRange_Row1 To sRange_Row2
    43.                 If Range(Chr(col) & Row).Locked Then 'HERE IS WHERE IT DOESNT SEEM TO WORK
    44.                      tWS.Range(Chr(lCol) & lRow) = WS.Name & "!" & Chr(col) & Row
    45.                      lRow = lRow + 1
    46.                 End If
    47.             Next
    48.         Next
    49.     End If
    50. Next
    51. End Sub
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  6. #6
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Scan for Locked Cells

    I think the problem is that when you are checking the cell to see if it is locked you are not refrencing the "WS" worksheet. Here's your code with a WITH statement added which ensures the .Range(Chr(col) & Row) refers to the correct sheet.

    Hope this is right after my last post here....

    VB Code:
    1. Sub ScanCell()
    2. Dim WB As Workbook
    3. Dim WS As Worksheet
    4. Dim tWS As Worksheet
    5. Set WB = ActiveWorkbook
    6. Dim sRange_Col As String
    7. Dim sRange_Row As Long
    8. Dim tmp As String
    9. Dim tmpR() As String
    10. Dim lRow As Integer
    11. Dim lCol As Integer
    12. lRow = 2
    13. lCol = 64
    14. WB.Worksheets.Add Sheets(1)
    15. Set tWS = ActiveSheet
    16. tWS.Name = "LOCKED CELLS"
    17. For Each WS In WB.Worksheets
    18.     With WS
    19.         If Left(.Name, 4) <> "LOCK" Then
    20.             tWS.Range("A1") = "LOCKED CELLS"
    21.             lRow = 2
    22.             lCol = lCol + 1
    23.             tmp = Replace(.UsedRange.Address, "$A$1:", "")
    24.             tmpR = Split(tmp, "$")
    25.             sRange_Col = tmpR(1)
    26.             sRange_Row = tmpR(2)
    27.             For col = 65 To Asc(sRange_Col)
    28.                 For Row = 1 To sRange_Row
    29.                     If .Range(Chr(col) & Row).Locked = True Then
    30.                          tWS.Range(Chr(lCol) & lRow) = WS.Name & "!" & Chr(col) & Row
    31.                          lRow = lRow + 1
    32.                     End If
    33.                 Next
    34.             Next
    35.         End If
    36.     End With
    37. Next
    38. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  7. #7

    Thread Starter
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Scan for Locked Cells

    you're right! good catch!
    changed it to:

    If WS.Range(Chr(col) & Row).Locked = True Then

    and it worked perfectly

    Thanks DKenny!
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width