【发布时间】:2018-03-04 07:23:25
【问题描述】:
在过去一周内刚开始使用 VBA,并且正在玩工作表与模块并定义变量。这是我目前拥有的代码:
表 1:
Dim writtenvoe As Boolean
Dim verbalvoe As Boolean
Dim voerequired As Boolean
Dim CA14 As Boolean
Dim CA15 As Boolean
Dim W2s As Boolean
Dim tranapp As Boolean
模块1:
Public Sub ImpDocs()
'Is written voe on file?
If Sheet1.CheckBox6.Value = True Or Sheet1.CheckBox8.Value = True Or Sheet1.CheckBox9.Value = True Or Sheet1.CheckBox10.Value = True Then writtenvoe = True Else writtenvoe = False
If Sheet1.CheckBox6.Value = True Then Sheet1.[O40] = "hello" Else Sheet1.[O40] = ""
If writtenvoe = True Then Sheet1.[O39] = "writtenvoe = true" Else Sheet1.[O39] = "writtenvoe=false"
'Is verbal voe on file?
If Sheet1.CheckBox11.Value = True Then verbalvoe = True
'Is a written VOE required?
If Sheet1.CheckBox16.Value = True Or Sheet1.CheckBox18.Value = True Or Not IsEmpty(Sheet1.[H33]) Then voerequired = True
If voerequired = True Then Sheet1.[J35] = "hello" Else Sheet1.[J35] = ""
'Are tax docs CA14 or CA15?
If Sheet1.CheckBox31.Value = True Or Sheet1.CheckBox7.Value = True Or Sheet1.CheckBox32.Value = True Or Sheet1.[H29].Value > 25 Then CA15 = True Else CA15 = False
If CA15 = False Then CA14 = True Else CA14 = False
'Are W-2's on file?
If Sheet1.CheckBox12.Value = True And Sheet1.CheckBox13.Value = True Then W2s = True
'Are 4506-T's on file?
If Sheet1.CheckBox60.Value = True And Sheet1.CheckBox61.Value = True Then tranapp = True
'Order Wage transcripts if W-2s and 4506-T not on file
If W2s = False And tranapp = False And CA14 = True Then Sheet4.fullCA14
End Sub
还有模块 1(第二个子程序):
Public Sub test()
If writtenvoe = True Then Sheet1.[N37] = "Yes" Else Sheet1.[N37] = "No"
End Sub
我注意到在这种当前格式下,Sub ImpDocs 运行良好,并且变量被正确确定,但 Sub test 总是返回 false。但是,如果我将所有代码放入模块 1,那么一切都会按预期工作。似乎只有 Sub 测试会受到在 Sheet1 和 Module1 中声明变量的影响。真的吗?如果是这样,为什么?
谢谢。
【问题讨论】:
-
也许尝试将 Option Explicit 放在模块的顶部以确保正确找到所有变量声明(尽管它不应该有所作为,因为 bool 的默认值为 false)?无论如何,最好使用此行,因为它会提醒您是否未定义变量。
-
这可能是一件显而易见的事情,但是,您已经在 if 语句中的所有复选框都设置为 false 的情况下对此进行了测试,我认为对吗?
-
@TiannaProcon 好建议,选项显式添加确实告诉我我的变量没有在测试中定义。你知道为什么 ImpDocs 能够提取变量,但 test 不能吗?我确实测试了两个程序中的变量,并且 ImpDocs 准确返回。