Using AutoFilter macro on a protected worksheet? VBA- Excel

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. 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!
 

Attachments

  • Before Clear Sold.JPG
    Before Clear Sold.JPG
    207.6 KB · Views: 10
  • Protected Clear Sold.JPG
    Protected Clear Sold.JPG
    126.5 KB · Views: 12
  • Debug error.JPG
    Debug error.JPG
    140.1 KB · Views: 12

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)

Forum statistics

Threads
1,224,812
Messages
6,181,105
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