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
Images showing what happens are attached as well. Any help would be greatly appreciated!
This is the code I am using
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
Images showing what happens are attached as well. Any help would be greatly appreciated!