Forum Discussion

olopa67's avatar
olopa67
Brass Contributor
Feb 18, 2022
Solved

select and delete value of only unlocked cell

I need to delete all entries of unprotected cell only in a specific area of the table (the one in gray)

i tried by record a macro using the find functionality via unlock format. if i manually perform Find unlocked cells then select all and press delete it works fine but if i record the macro with exactly the same steps it will delete the all area if the worksheet is unprotected or if i protect the worksheet i got a debug error saying i m trying  to delete protected cells.

any help, solutions or strategy will be highly appreciated 

thank you for time

 

 

 

 

 

 

 

  • olopa67 

     

    Perhaps try this. But, it's not clear if your table headers are word wrapped with just a space in between the words or if there is a carriage return (Alt+Enter). If you used Alt+Enter, then you'll need to reference the range like: 

     

    Range ("Table1[[WED" & Chr(10) & "START]:[TUE" & Chr(10) & "SECTION]]")

     

    Change table name from Table1 to whatever yours is called.

     

     

    Sub ClearUnlocked()
         Dim searchRange As Range
         Dim unlockedCells As Range
         Dim foundCell As Range
         Dim firstAddress As String
         
         
         On Error GoTo ErrHandler
         
         Set searchRange = Worksheets("DETAILED ROSTER").Range("Table1[[WED START]:[TUE SECTION]]")
         
         Application.FindFormat.Locked = False
         
         With searchRange
              Set foundCell = .Find(What:="", after:=.Cells(1), SearchFormat:=True)
              
              If foundCell Is Nothing Then
                   Err.Raise Number:=vbObjectError + 513, Description:="No unlocked cells found."
              End If
              
              Set unlockedCells = foundCell
              firstAddress = foundCell.Address
              
              Do
                   DoEvents
                   Set foundCell = .Find(What:="", after:=foundCell, SearchFormat:=True)
                   Set unlockedCells = Union(unlockedCells, foundCell)
              Loop Until foundCell.Address = firstAddress
         End With
         
         If Not unlockedCells Is Nothing Then
              unlockedCells.ClearContents
         End If
         
         
    ExitProc:
         Exit Sub
         
    ErrHandler:
         MsgBox "Error " & Err.Number & ": " & Err.Description
         Resume ExitProc
         
    End Sub

     

     

2 Replies

  • JMB17's avatar
    JMB17
    Bronze Contributor

    olopa67 

     

    Perhaps try this. But, it's not clear if your table headers are word wrapped with just a space in between the words or if there is a carriage return (Alt+Enter). If you used Alt+Enter, then you'll need to reference the range like: 

     

    Range ("Table1[[WED" & Chr(10) & "START]:[TUE" & Chr(10) & "SECTION]]")

     

    Change table name from Table1 to whatever yours is called.

     

     

    Sub ClearUnlocked()
         Dim searchRange As Range
         Dim unlockedCells As Range
         Dim foundCell As Range
         Dim firstAddress As String
         
         
         On Error GoTo ErrHandler
         
         Set searchRange = Worksheets("DETAILED ROSTER").Range("Table1[[WED START]:[TUE SECTION]]")
         
         Application.FindFormat.Locked = False
         
         With searchRange
              Set foundCell = .Find(What:="", after:=.Cells(1), SearchFormat:=True)
              
              If foundCell Is Nothing Then
                   Err.Raise Number:=vbObjectError + 513, Description:="No unlocked cells found."
              End If
              
              Set unlockedCells = foundCell
              firstAddress = foundCell.Address
              
              Do
                   DoEvents
                   Set foundCell = .Find(What:="", after:=foundCell, SearchFormat:=True)
                   Set unlockedCells = Union(unlockedCells, foundCell)
              Loop Until foundCell.Address = firstAddress
         End With
         
         If Not unlockedCells Is Nothing Then
              unlockedCells.ClearContents
         End If
         
         
    ExitProc:
         Exit Sub
         
    ErrHandler:
         MsgBox "Error " & Err.Number & ": " & Err.Description
         Resume ExitProc
         
    End Sub

     

     

    • olopa67's avatar
      olopa67
      Brass Contributor
      thank you JMB17 it is working perfectly
      really appreciated your help

Resources