【发布时间】:2013-07-29 10:56:22
【问题描述】:
我认为这可能是一个快速的解决方案,但它整个上午都难住了我。
我在 vba 项目的顶部将许多变量定义为公共变量(任何以“yn”结尾的变量都是 String,其余的是 Variant 或 Single)。整个项目中的不同子通过一系列问题和答案来定义这些变量。现在,当我尝试将定义的变量编译成方程时,最后出现错误。
上周我已经用不同的最终方程完成了同样的过程,而且效果很好。我检查了每个变量以确保它输入正确,并且我看不出代码有任何问题。请帮忙!谢谢!
Public Sub Rth1 ()
Dim Red1th as single
If Width1thyn = vbYes Then
If Length1thyn = vbYes Then
If V1turnspeedyn = vbYes Then
Red1th = ((Width1th + 20) / (1.47 * 20)) - 1
Else
Red1th = ((Width1th + 20) / (1.47 * V1turnspeedstudy)) - 1
End If
Else
If V1turnspeedyn = vbYes Then
Red1th = ((Width1th + Length1thstudy) / (1.47 * 20)) - 1
Else
Red1th = ((Width1th + Length1thstudy) / (1.47 * V1turnspeedstudy)) - 1
End If
End If
Else
If Length1thyn = vbYes Then
If V1turnspeedyn = vbYes Then
Red1th = ((Width1thstudy + 20) / (1.47 * 20)) - 1
Else
Red1th = ((Width1thstudy + 20) / (1.47 * V1turnspeedstudy)) - 1
End If
Else
If V1turnspeedyn = vbYes Then
Red1th = ((Width1thstudy + Length1thstudy) / (1.47 * 20)) - 1
Else
Red1th = ((Width1thstudy + Length1thstudy) / (1.47 * V1turnspeedstudy)) - 1
End If
End If
End If
MsgBox "Red Clearance Interval is " & Red1th, "vbOkOnly", "Red Clearance Interval"
End Sub
【问题讨论】:
-
vbYes是 6。如果您的...yn数据类型是字符串,那么比较时您可能会得到一些奇怪的结果。您是否尝试过Option Explicit以确保您没有拼写错误? -
我尝试将所有“yn”变量转换回整数而不是字符串,但它没有改变任何东西。我也刚刚添加了 Option Explicit,但它没有找到任何东西。我仍然遇到同样的错误,现在之前有效的另一个最终方程式也不起作用。
-
如果在使用 Option Explicit 时出现问题,那么您没有在任何地方使用
Dimmed 变量。如果程序在方程上失败,我会检查变量的定义。vartype应该可以帮助您找出 type of data 变量期望/使用的内容 -
哪一行会产生错误?
标签: vba if-statement global-variables public