我有这个调用其他程序的主程序。但在两个地方,我需要检查所有值是否正确。如果不是,我希望这个主程序退出。我要做的是检查我的子过程中的值,如果它们不正确,将exitall更改为true,这将导致子过程停止。问题是,我相当肯定,如果我在我的子程序中说将exitall的值更改为true,它将不会影响我的主程序。
我的问题是,如果在我的子程序中更改了exitall,如何在主程序中进行更改?
谢谢。
Sub Allstepstogether()
Dim r As Integer
Dim exitall As Boolean
Dim answer As Variant
Dim hda As Boolean
Dim wdfh As Variant
hda = False
exitall = False
Call Clean
For Each cell In ThisWorkbook.Sheets("Heyaa").Range("C2:C15")
If Weekday(Date) = vbMonday Then
If CDate(cell.Value) = Date - 3
hda = True
wdfh = cell.Offset(0, 1).Value
End If
Else
If CDate(cell.Value) = Date - 1 Then
hda = True
wdfh = cell.Offset(0, 1).Value
End If
End If
Next cell
Call step4
r = Application.WorksheetFunction.CountA(ThisWorkbook.Sheets("BlaCheck").Range("A1:A150"))
If r <> 100 Then
answer = MsgBox("Data not yet uploaded, try again later.", vbOKOnly)
Exit Sub
Else
Call step5
If exitall = True Then
Exit Sub
Else
Call Step7alloptions
Call step8
Call Timetocheck
If exitall = True Then
Exit Sub
Else
Call Step9
Call Step10
Call Step11
End If
End If
End If
end sub
Step5的一部分,它应该将exitall更改为true,从而在主程序不正确时停止执行主程序。
sub Step5
dim exitall as boolean
dim lr as integer
'....
'code
'....
lr = Application.WorksheetFunction.CountA(ThisWorkbook.Sheets("BlaCheck").Range("A1:A500"))
If lr > 100 Then
answer = MsgBox("Ups, It did not run correctly, this code execution will be terminated", vbOKOnly)
exitall = True
Exit Sub
End If
end sub
答案 0 :(得分:1)
选项1是使用全局范围声明exitall
:
Dim exitall As Boolean '<--Add this at the top of the module.
Sub Allstepstogether()
Dim r As Integer
'Dim exitall As Boolean '<--Remove this from all your Subs.
最好将Sub
更改为Function
,以便成功返回Boolean
,然后测试一下。请注意,在退出If条件后,您也不必使用Else - 这应该会显着降低缩进级别:
转换子:
Function Step5() As Boolean
Dim lr As Integer
'....
'code
'....
lr = Application.WorksheetFunction.CountA(ThisWorkbook.Sheets("BlaCheck").Range("A1:A500"))
If lr > 100 Then
answer = MsgBox("Ups, It did not run correctly, this code execution will be terminated", vbOKOnly)
Exit Function
End If
Step5 = True
End Function
致电代码:
Sub Allstepstogether()
'[Snip]
r = Application.WorksheetFunction.CountA(ThisWorkbook.Sheets("BlaCheck").Range("A1:A150"))
If r <> 100 Then
answer = MsgBox("Data not yet uploaded, try again later.", vbOKOnly)
Exit Sub
End If
If Not Step5 Then Exit Sub
Step7alloptions
step8
If Not Timetocheck Then Exit Sub
Step9
Step10
Step11
End Sub