There is nothing wrong with your code. You will only get this error if the Active
worksheet is password protected.
Also it is a much better option to avoid using .Select
and ActiveSheet
. Your code can be written as
Sub DuplicateRemove()
Dim ws As Worksheet
Set ws = Sheets("Sheet1")
With ws
If .ProtectContents = True Then
MsgBox "Worksheet is protected"
.Unprotect "MYPASSWORD"
.Range("$A$1:$A$95678").RemoveDuplicates Columns:=1, Header:=xlNo
.Protect "MYPASSWORD"
Else
.Range("$A$1:$A$95678").RemoveDuplicates Columns:=1, Header:=xlNo
End If
End With
End Sub
FOLLOWUP
Sub DuplicateTest()
ActiveSheet.Columns(1).RemoveDuplicates Columns:=1, Header:=xlNo
End Sub
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…