在代码中设置一些表单时出错,但与其他表单不一致

我有一个自动化我的公司的发票的macros。 有时我们有forms发票,这意味着我们删除了三个被操纵的发票中的两个(客户,所有者和增值税)。 因此,在每个代码运行之前,我必须检查是否存在表单。

我碰到的问题是,当我设置一些要检查的工作表时,它会给我一个运行时错误424 。 在下面的代码中,这是第二次,TVA工作表检查错误发生的地方( If TVASheet Is Nothing Then )。 请注意,我有几乎完全相同的代码,除了检查ClientSheet。

 'If the current payment is the only payment then we add to the TVA invoice 'that the rest is due 'on the date the balance of rental is due If (Range("F3") <> "" And Range("G3") = "" And Range("H3") = "") Then On Error Resume Next Set TVASheet = Sheets("TVA Invoice") On Error GoTo 0 If TVASheet Is Nothing Then Else Sheets("TVA Invoice").Select If Cells.Find(What:="du paiement sera reçu", After:=ActiveCell, LookIn:= _ xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _ xlNext, MatchCase:=False, SearchFormat:=False) Is Nothing Then Cells.Find(What:="Select!F3", After:=ActiveCell, LookIn:=xlFormulas, _ LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False).Activate ActiveCell.Offset(1, 0).Select Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove Selection.Font.Bold = False ActiveCell = _ "=100 - 100*TEXT(Select!F3,""0%"")&""% du paiement sera reçu le ""&PROPER(TEXT(Select!C30,""JJ-MMMM-AAAA""))&""""" Else End If End If Else On Error Resume Next Set TVASheet = Sheets("TVA Invoice") On Error GoTo 0 If TVASheet Is Nothing Then Else Sheets("TVA Invoice").Select If Cells.Find(What:="du paiement sera reçu", After:=ActiveCell, LookIn:= _ xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _ xlNext, MatchCase:=False, SearchFormat:=False) Is Nothing Then Else Cells.Find(What:="du paiement sera reçu", After:=ActiveCell, LookIn:=xlFormulas, _ LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False).Activate ActiveCell.Rows("1:1").EntireRow.Select Selection.Delete Shift:=xlUp End If End If End If Sheets("Select").Select 

在代码的开头声明您的TVASheetvariables:

 Dim TVASheet As Worksheet 

很明显,“TVA发票”表没有find,并且Set命令失败。 由于你的On Error Resume Next ,程序继续下去,解释器不知道你的If子句中应该包含什么TVASheet 。 你可以通过声明来避免这种情况。