【问题标题】:VBA If statements with public variables error type mismatch (error 13)带有公共变量错误类型不匹配的 VBA If 语句(错误 13)
【发布时间】: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

【问题讨论】:

  • vbYes6。如果您的...yn 数据类型是字符串,那么比较时您可能会得到一些奇怪的结果。您是否尝试过Option Explicit 以确保您没有拼写错误?
  • 我尝试将所有“yn”变量转换回整数而不是字符串,但它没有改变任何东西。我也刚刚添加了 Option Explicit,但它没有找到任何东西。我仍然遇到同样的错误,现在之前有效的另一个最终方程式也不起作用。
  • 如果在使用 Option Explicit 时出现问题,那么您没有在任何地方使用 Dimmed 变量。如果程序在方程上失败,我会检查变量的定义。 vartype 应该可以帮助您找出 type of data 变量期望/使用的内容
  • 哪一行会产生错误?

标签: vba if-statement global-variables public


【解决方案1】:

vbYesMsgBox返回时使用。

如果您的变量不依赖于MsgBox 返回,请考虑使用Boolean 或然后分配您自己的枚举(避免使用vb 开始名称),

确保在之前代码的赋值中使用vbYes

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-19
    • 1970-01-01
    • 2021-09-22
    • 1970-01-01
    • 2015-10-31
    • 1970-01-01
    • 2018-02-16
    相关资源
    最近更新 更多