Error when trying to call protect/unprotect macro within code

TheGrandPooba

New Member
Joined
Jul 1, 2022
Messages
12
Office Version
  1. 365
Platform
  1. Windows
Hi all,
I keep getting "Compile Error: Member already exists in an object module from which this object module derives" when trying to run this code:
VBA Code:
Const strPassword As String = "Soybean?"

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge > 1 Then Exit Sub
    If Target.Address(0, 0) <> "G3" Then Exit Sub
    Dim srcWS As Worksheet, desWS As Worksheet, rng As Range
    Set srcWS = Worksheets("OrderImport")
    Set desWS = Worksheets("OrderHistory")
   
   
     Call Protection(srcWS, False)
     Call Protection(desWS, False)
    Application.ScreenUpdating = False
   
    With srcWS.ListObjects("OImport")
        .Range.AutoFilter 1, Target.Value
        .DataBodyRange.SpecialCells(xlCellTypeVisible).Copy desWS.Cells(desWS.Rows.Count, "A").End(xlUp).Offset(1)
        Set rng = .DataBodyRange.SpecialCells(xlCellTypeVisible).Cells
        .DataBodyRange.AutoFilter Field:=1
        rng.Delete
    End With
   
    Call Protection(srcWS, True)
    Call Protection(desWS, True)
   
    Application.ScreenUpdating = True
End Sub
Sub Protection(proWS As Worksheet, PAction As Boolean)
    If PAction = True Then
        proWS.Protect Password:=strPassword
    Else
        proWS.Unprotect Password:=strPassword
    End If
End Sub
The error highlights 'Sub Protection(proWS As Worksheet, PAction As Boolean)'

The main chunk of the code is from my last post, VBA to lookup order#, Lookup Order#, Copy Paste data into history, then delete orders
The generous code from mumps hadn't accounted for unprotecting/re-protecting the OrderImport and OrderHistory tabs.
Can anyone help figure out how to unprotect the OrderImport/OrderHistory tabs, then reprotect them after the With loop runs?
Thanks all, I appreciate you
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Give the sub "Protection" another name ("PassProtection" would be a good suggestion), it's only repeated five times in the module and have a try.
 
Upvote 0
Solution
"Protect" is a keyword in Excel, it's not recommended to use it as the name of a Macro or Function.
Thanks for the positive feedback(y), glad having been of some help.
 
Upvote 0

Forum statistics

Threads
1,215,518
Messages
6,125,292
Members
449,218
Latest member
Excel Master

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