Excel VBA工作簿保护时删除重复,即使单元格未locking

Excel 2010 – 我有一个标签为“DATA1”的工作表,在这个例子中,整个工作簿已经被保护,用户只能使用特定的单元格。 即使我有列(“L:N”),我想删除解锁重复,代码将不会删除重复。 考虑到代码必须经过的所有操作,对整个工作簿进行解保护和重新保护的方法并不是一个可行的select。 供参考:当pipe理员有整个文件不受保护,代码运行完美。

它挂在下面的代码与此错误信息 – “运行时错误'1004':应用程序定义的或对象定义的错误:

ActiveSheet.Range("L1:N200").RemoveDuplicates Columns:=Array(1, 2, 3), _ Header:=xlYes 

解锁整个工作表后,甚至认为整个工作簿都受到保护,现在停止在第一行代码上:

 Sheets("DATA1").Range("List_FTE_Names").ClearContents 

这是我的代码:

  Sub mcr_FTE_Names() 'Clear contents in the FTE Names columns for a clean slate Sheets("DATA1").Range("List_FTE_Names").ClearContents 'Copy FTE Names columns from Labor Forecast Detail Sheets("Labor Forecast Detail").Range("List_FTE_Names_Forecast").Copy 'Paste Special Values of data just copied Sheets("DATA1").Range("List_FTE_Names").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False Application.CutCopyMode = False 'Remove Duplicates Sheets("DATA1").Range("List_FTE_Names").RemoveDuplicates Columns:=Array(1, 2, 3), _ Header:=xlYes 'Sort Alphabetically ActiveWorkbook.Worksheets("DATA1").Sort.SortFields.Clear ActiveWorkbook.Worksheets("DATA1").Sort.SortFields.Add Key:=Range("L2:L800") _ , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal ActiveWorkbook.Worksheets("DATA1").Sort.SortFields.Add Key:=Range("M2:M800") _ , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal ActiveWorkbook.Worksheets("DATA1").Sort.SortFields.Add Key:=Range("N2:N800") _ , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal With ActiveWorkbook.Worksheets("DATA1").Sort .SetRange Range("List_FTE_Names") .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With MsgBox "Your Resources have been consolidated and alphabetized," & vbNewLine & "you will now proceed back to the Home page." Sheets("Home").Select Range("A1").Select End Sub 

任何想法和/或方向将不胜感激。 感谢您的期待。

这是我的ProtectAll_Admin()代码,如果有帮助:

 Sub ProtectAll_ADMIN() Dim ws As Worksheet Dim pWord1 As String Dim pWord2 As String For Each ws In Worksheets If ws.ProtectContents Then MsgBox ActiveWorkbook.Name & " is already protected.", vbCritical Exit Sub End If Next ws ' To Hide all rows and columns for editing Call mcr_HideRowsColumns_ADMIN pWord1 = InputBox("Please Enter the password") If pWord1 = "" Then Exit Sub pWord2 = InputBox("Please re-enter the password") If pWord2 = "" Then Exit Sub 'Make certain passwords are identical If InStr(1, pWord2, pWord1, 0) = 0 Or InStr(1, pWord1, pWord2, 0) = 0 Then MsgBox "You entered different passwords. No action taken!" Exit Sub End If For Each ws In Worksheets ws.Protect Password:=pWord1 'ws.Protect UserInterfaceOnly:=True ws.Protect AllowFiltering:=True Next MsgBox "All Sheets have been Protected" & vbNewLine & "and the File is ready for the PM." End Sub 

当工作表受到保护时,也许尝试使用userinterfaceonly方法。 即

 for each ws in workbook ws.Protect UserInterfaceOnly:=True next ws 

这个代码只locking用户而不是vba。 您不必担心在vba代码中保护和取消保护工作表。