Excel宏每隔两次给出运行时错误'1004'

时间:2019-10-17 17:05:08

标签: excel vba

我的宏每运行一次,就会产生“运行时错误'1004':应用程序定义的错误或对象定义的错误”。错误发生在cel.Validation下的“ .Add”上。

最初,代码在每次尝试时都会给出错误。经过一番研究,我发现启用保护时可能会出现问题,因此代码的注释部分已被删除。

此代码检查其他工作表上的值,并且应该在满足某些条件的情况下设置验证。

Private Sub CommandButton1_Click()

    Dim Table1Check As Range
    Dim Table2Check As Range
    Dim LocalCells As Range
    Dim cellCheck As Range
    Dim cellAddr As String

    Set Table1Check = Worksheets("Table1").Range("A16:AC467")
    Set Table2Check = Worksheets("Table2").Range("A16:AC467")
    Set LocalCells = ActiveSheet.Range("R13:R464")

    ActiveSheet.Unprotect "<pw>"
    ActiveSheet.Range("R13:R464").Locked = False
    ActiveSheet.CheckBox1.Value = False
    'ActiveSheet.EnableOutlining = True
    'ActiveSheet.Protect Password:="<pw>", UserInterFaceOnly:=True, AllowFiltering:=True, AllowFormattingColumns:=True

    For Each cel In LocalCells

        Test0 = WorksheetFunction.VLookup(Cells(cel.Row, cel.Column + 1), Table1Check, 16, False)
        cellAddr = ActiveSheet.Cells(cel.Row + 3, cel.Column - 3).Address(RowAbsolute:=False, ColumnAbsolute:=False)
        Set cellCheck = Worksheets("Table2").Range(cellAddr)

        If Test0 = "Option1" Or Test0 = "Option2" Then
            cel.Interior.Color = xlNone
            cel.Interior.Pattern = xlPatternNone
            If cellCheck.Interior.Pattern <> xlPatternUp Then
                With cel.Validation
                    .Delete
                    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
                        Formula1:="A,B"
                End With
            Else
                With cel.Validation
                    .Delete
                    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
                        Formula1:="A"
                End With
            End If
        ElseIf cellCheck.Interior.Pattern <> xlPatternUp Then
            cel.Interior.Color = xlNone
            cel.Interior.Pattern = xlPatternNone
            With cel.Validation
                .Delete
                .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
                    Formula1:="B"
            End With
        Else
            cel.Interior.Color = xlNone
            cel.Interior.Pattern = xlPatternUp
            cel.Interior.PatternColor = RGB(0, 0, 0)
            cel.Validation.Delete
        End If



    Next cel

    'ActiveSheet.Unprotect "<pw>"
    ActiveSheet.Range("R13:R464").Locked = True
    ActiveSheet.CheckBox1.Value = True
    ActiveSheet.EnableOutlining = True
    ActiveSheet.Protect Password:="<pw>", UserInterFaceOnly:=True, AllowFiltering:=True, AllowFormattingColumns:=True

End Sub

关于保护,这是打开工作簿时如何保护每个工作表的方式:

Private Sub Workbook_Open()

    Dim ws As Worksheet

    For Each ws In ThisWorkbook.Worksheets

        ws.EnableOutlining = True

        If ws.Name = "Version" Then
            ws.Protect Password:="<pw>", UserInterFaceOnly:=True, AllowFiltering:=True, AllowFormattingColumns:=True, AllowInsertingRows:=True
        Else
            ws.Protect Password:="<pw>", UserInterFaceOnly:=True, AllowFiltering:=True, AllowFormattingColumns:=True
        End If

    Next ws

End Sub

如果这是一个简单的问题,请原谅我的无知(我的excel-vba技能是自学的)。我尝试搜索其他解决方案,但找不到任何解决方案。我不明白为什么它可以隔行执行。

更新:我刚刚发现,如果在检查CommandButton1的同时单击CheckBox1,则宏将失败。如果未选中CheckBox1,则可以使用。这就解释了为什么它每隔一次就起作用,因为一次失败,我的宏没有完成,因此它从未到达ActiveSheet.CheckBox1.Value = True行。因此,下次我运行它时,CheckBox1未被选中。我仍然不明白为什么会发生这种情况,因为我在for循环开始之前先在代码中“取消选中”:ActiveSheet.CheckBox1.Value = False

1 个答案:

答案 0 :(得分:0)

基于我的UPDATE,我能够找到解决方案。我不确定100%发生了什么,但是如果我注释掉这两行,现在可以了:

ActiveSheet.CheckBox1.Value = False
ActiveSheet.CheckBox1.Value = True

可能是因为我的宏在干扰我,因为CheckBox1_Click()代码锁定/解锁了LocalCells范围。更改复选框的值会激活其Click宏吗?