【问题标题】:VBA Conditional If Then Variable modificationVBA 条件 If Then 变量修改
【发布时间】:2023-03-08 07:41:01
【问题描述】:

我正在尝试使用“_”将来自用户输入表单的信息链接在一起,并有条件地从第二个变量中减去一个。这是我所拥有的:

If strckbx = True & strmonth = "oct" Then
 Range("C1") = strmonth + "_" + (strfiscalyear - 1)
 Else
 Range("C1") = strmonth + ("_") + strfiscalyear
End If

If strckbx = True & strmonth = "Nov" Then
 Range("C1") = strmonth + ("_") + (strfiscalyear- 1)
 Else
 Range("C1") = strmonth + ("_") + strfiscalyear
End If

If strckbx = True & strmonth = "dec" Then
 Range("C1") = strmonth + ("_") + (strfiscalyear - 1)
 Else
 Range("C1") = strmonth + ("_") + strfiscalyear
End If

减一取决于用户表单中的复选框。当我尝试运行或测试它时,我不断收到运行时错误 13:类型不匹配。有谁知道我能做什么?

【问题讨论】:

    标签: forms excel vba checkbox


    【解决方案1】:

    应该是And 而不是&

    例如

    If strckbx = True And strmonth = "oct" Then
    

    & 在 VBA 中用于连接。

    在这里阅读更多:

    【讨论】:

      【解决方案2】:

      你不能从一个字符串中减去一个数字。这会给你一个类型不匹配。

      改为:cstr(cint(strfiscalyear) - 1)

      另外,考虑使用And 代替&

      【讨论】:

      • Range("C1") = strmonth & "_" & (strfiscalyear) - 1
      猜你喜欢
      • 2013-04-11
      • 1970-01-01
      • 1970-01-01
      • 2021-06-07
      • 1970-01-01
      • 2021-12-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      相关资源
      最近更新 更多