Use AutoFilter macro on protected worksheet?

cheesypoofs

New Member
Joined
Mar 7, 2023
Messages
9
Office Version
  1. 365
Platform
  1. 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
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!
 

Attachments

  • Before Clear Sold.JPG
    Before Clear Sold.JPG
    207.6 KB · Views: 13
  • Protected Clear Sold.JPG
    Protected Clear Sold.JPG
    126.5 KB · Views: 13

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Hello. Hit Debug button and share a screenshot with the line creating the error.
Here you go! Keep in mind, this only happens on a protected sheet. If it is unprotected, the macro works as it is supposed to
 

Attachments

  • Debug error.JPG
    Debug error.JPG
    140.1 KB · Views: 12
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,126
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top