我有一个大型宏用于自动为我公司开具发票。有时我们有形式发票,这意味着我们删除了三个被操纵的发票中的两个(客户,所有者和增值税)。因此,我必须在每个代码运行之前检查工作表是否存在。
我遇到的问题是,当我设置一些要检查的工作表时,它会给我一个运行时错误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
答案 0 :(得分:1)
在代码的开头声明您的TVASheet
变量:
Dim TVASheet As Worksheet
显然是&#34; TVA发票&#34;找不到工作表,Set
命令失败。由于您的On Error Resume Next
程序仍在继续,并且解释器不知道TVASheet
条款中If
应该是什么。您可以通过声明来避免这种情况。