Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 为什么在错误处理子系统中调整条件格式时会出现1004错误?_Excel_Vba_Error Handling - Fatal编程技术网

Excel 为什么在错误处理子系统中调整条件格式时会出现1004错误?

Excel 为什么在错误处理子系统中调整条件格式时会出现1004错误?,excel,vba,error-handling,Excel,Vba,Error Handling,因此,我试图将excel工作表与其他宏的错误隔离开来,因为我丢失了所有变量定义 所以,我有一个在工作簿open上运行的初始化子例程。我将其定义为模块中的子模块,现在将工作簿中的子模块称为“打开”。我还在错误处理例程中调用sub。以下是模块代码: Option Explicit Public Fixed As Range Public Motor As Range Public Engine As Range Public Stage1 As Range Public Stage2 As Ran

因此,我试图将excel工作表与其他宏的错误隔离开来,因为我丢失了所有变量定义

所以,我有一个在工作簿open上运行的初始化子例程。我将其定义为模块中的子模块,现在将工作簿中的子模块称为“打开”。我还在错误处理例程中调用sub。以下是模块代码:

Option Explicit

Public Fixed As Range
Public Motor As Range
Public Engine As Range
Public Stage1 As Range
Public Stage2 As Range
Public Stage3 As Range
Public Drive As Range
Public Stage As Range
Public Required As Range
Public SaveChk As Integer
Public SaveName As String
Public UserPath As String
Public Path As String
Public SaveError As Integer
Public EditChk As String
Public FormRange As Range

Sub Initialize()

'Set all of the cell check and formatting ranges
Set Module1.FormRange = ThisWorkbook.Worksheets("Field Service Report") _
    .Range("A1:AL59")
Set Module1.Fixed = ThisWorkbook.Worksheets("Field Service Report") _
    .Range("I7,I8,Z7,E6,P6,X6,A45,A57,F57,L57,P57,V56,V57,X57,D28,D29,O28")
Set Module1.Motor = ThisWorkbook.Worksheets("Field Service Report") _
    .Range("AA28,AA29,Z30,AE30,AC31,AJ29,AK30,AK31")
Set Module1.Engine = ThisWorkbook.Worksheets("Field Service Report") _
    .Range("D15,M15,T15,AE15,AJ15,T17,AE17,AH17,J23,J24,J25,AA20,AA21,AA22,AA23,AA24,AA25,AJ20,AJ21,AJ22,AJ23,AJ24,AJ25")
Set Module1.Stage1 = ThisWorkbook.Worksheets("Field Service Report") _
    .Range("H37,N37,AA37,AG37,AG38,H38,K38,N38,Q38,AA38,AD38,AJ38")
Set Module1.Stage2 = ThisWorkbook.Worksheets("Field Service Report") _
    .Range("H39,N39,AA39,AG39,AG40,H40,K40,N40,Q40,AA39,AA40,AD40,AJ40")
Set Module1.Stage3 = ThisWorkbook.Worksheets("Field Service Report") _
    .Range("H41,N41,AA41,AG41,H42,N42,Q42,AA42,AG42,AJ42")

'Disabling Drag and Drop for Unprotected Cells
Application.CellDragAndDrop = False

'Set the start parameters of the worksheet
Module1.EditChk = "N"   'Set to Y to allow saving when not complete (debugging), Set to N for standard'
Module1.SaveChk = 0
Module1.SaveError = 0
Module1.FormRange.FormatConditions.Delete
With ThisWorkbook.Worksheets("Field Service Report")
    .Range("A1:AL59") _
        .ColumnWidth = 2
    .PageSetup.PrintArea = "A1:AL59"
End With
Sheet1.CheckBox26.Value = True
Sheet1.CheckBox18.Value = False
With Module1.Motor.FormatConditions _
    .Add(Type:=xlBlanksCondition)
    .Interior.Color = vbRed
    .StopIfTrue = True
End With
Sheet1.Shapes("Option Button 134").ControlFormat.Value = xlOn
Module1.Stage2.FormatConditions _
    .Delete
Module1.Stage3.FormatConditions _
    .Delete
With Module1.Stage1.FormatConditions _
    .Add(Type:=xlBlanksCondition)
    .Interior.Color = vbRed
    .StopIfTrue = True
End With
Set Module1.Stage = Module1.Stage1
Module1.Fixed.FormatConditions.Delete
With Module1.Fixed.FormatConditions _
    .Add(Type:=xlBlanksCondition)
    .Interior.Color = vbRed
    .StopIfTrue = True
End With

End Sub
我还捕获粘贴命令并将其转换为粘贴特殊值。当用户尝试从一个合并单元格粘贴到不同大小的合并单元格时,会出现典型的Excel错误。当初始化例程从我的错误句柄运行时,运行到一半时,1004个错误发生在:

此工作簿。工作表(“现场服务报告”)。范围(“A1:AL59”)。格式条件。删除

如果删除该行,则在下一个条件格式命令中会出现错误。总而言之:打开工作簿时没有错误,但在错误处理期间子调用时出现1004错误

EH码

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

'Error Handling when workbook is unprotected so it doesn't lose it's protection on an error.
On Error GoTo eh

Dim lastAction As String

'Get the last action performed by user
lastAction = Application.CommandBars("Standard").Controls("&Undo").List(1)

'Check if the last action was a paste
If Left(lastAction, 5) = "Paste" Then
    'Without disabling events, the undone command will redo right after undo
    Application.EnableEvents = False
    Application.Undo
    Target.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone
    Application.EnableEvents = True

End If

Exit Sub

eh:
'Renables protection on error.
Call Initialize
Application.EnableEvents = True
ThisWorkbook.Sheets("Field Service Report").Protect Password:="x", UserInterfaceOnly:=True

End Sub

你能告诉我们在哪里调用
Initialize
的错误处理方法吗?所以,在混乱之后,它肯定与它在子例程中在模块中定义这一事实有关。如果我将有问题的代码复制到工作簿的“打开”子文件夹中,它可以正常工作。那么,现在哪一行出现错误?错误是什么?463和9,或者只是9?抱歉,必须重置所有内容并重新运行。所以…当从工作簿打开调用初始化时。没有问题。当从工作簿调用initialize时,我仍然在编辑的Module1.FormRange.FormatConditions.Delete行上收到1004运行时错误。如果您正在处理错误,您不会丢失全局变量。这将发生在未处理的错误上。