即使单元格未锁定,Excel VBA也会在工作簿受保护时删除重复项

时间:2017-05-16 18:12:46

标签: excel vba excel-vba

Excel 2010 - 我有一个标有“DATA1”的工作表,在这个例子中,整个工作簿都受到保护,用户只能使用某些单元格。即使我有列(“L:N”),我想在Un-Locked上删除重复项,代码也不会删除重复项。考虑到代码必须经历的所有操作,取消保护整个工作簿并重新保护的方法不是可行的选择。仅供参考:当管理员不保护整个文件时,代码运行完美。

它挂起以下代码并显示以下错误消息 - “运行时错误'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

1 个答案:

答案 0 :(得分:1)

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

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

此代码仅为用户锁定,但不为vba锁定。您无需担心在vba代码中保护和取消保护工作表。