cheesypoofs
New Member
- Joined
- Mar 7, 2023
- Messages
- 9
- Office Version
- 365
- Platform
- Windows
I am using the ActiveSheet.Unprotect and Protect codes at the beginning of all my macros, there are 4 total in my workbook. All of them work, except the one AutoFilter code I have to clear contents of table rows based on 3 different criteria. For some reason, I get an error when it filters to show the data to be cleared. Am I putting the unprotect and protect codes in the wrong place?
This is the code I am using. I will also include photos of before macro is ran, when the error comes up, and what is shown when I open the debugger
I tried using ActiveSheet.Unprotect and ActiveSheet.Protect at the beginning and end of the sub. Since this code is intended to filter the range, clear those contents, then unfilter, I am not sure if I am needing to add a second set of unprotect/protect codes? I feel like it is something very simple that I am missing... I am wanting it to go through that process on a password protected sheet so that users may modify only the allowed portions of the sheet to avoid any issues with the code accidentally being deleted in any part of the sheet. Any help would be greatly appreciated!
This is the code I am using. I will also include photos of before macro is ran, when the error comes up, and what is shown when I open the debugger
VBA Code:
Sub ClearSold()
ActiveSheet.Unprotect Password:="123"
Dim LO As ListObject
Dim dRng As Range
Dim CA As Variant, TA As Variant, N As Variant
Dim I As Long, J As Long, RRow As Long, RCol As Long
Set LO = ActiveSheet.ListObjects("Table5")
With LO
.Range.AutoFilter Field:=7, Criteria1:=Array("Cancelled", "Installed", "Scheduled"), Operator:=xlFilterValues
If WorksheetFunction.Subtotal(2, .DataBodyRange) > 0 Then
On Error Resume Next
Set dRng = .DataBodyRange.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
.Range.AutoFilter
If Not dRng Is Nothing Then
dRng.ClearContents
End If
End If
.Range.AutoFilter
End With
CA = LO.DataBodyRange.Value
TA = CA
I = 0
For RRow = LBound(CA, 1) To UBound(CA, 1)
J = 0
N = Trim(CA(RRow, LBound(CA, 2)))
If N <> "" Then
I = I + 1
End If
For RCol = LBound(CA, 2) To UBound(CA, 2)
TA(RRow, RCol) = vbNullString
N = Trim(CA(RRow, RCol))
If N <> "" Then
J = J + 1
TA(I, J) = CA(RRow, RCol)
End If
Next RCol
Next RRow
LO.DataBodyRange.Value = TA
ActiveSheet.Protect Password:="123"
End Sub
I tried using ActiveSheet.Unprotect and ActiveSheet.Protect at the beginning and end of the sub. Since this code is intended to filter the range, clear those contents, then unfilter, I am not sure if I am needing to add a second set of unprotect/protect codes? I feel like it is something very simple that I am missing... I am wanting it to go through that process on a password protected sheet so that users may modify only the allowed portions of the sheet to avoid any issues with the code accidentally being deleted in any part of the sheet. Any help would be greatly appreciated!