运行时错误13输入不匹配〜当Application.Inputbox取消时

安东·弗洛洛夫(Anton Frolov)

我已经阅读了有关此问题的一些问题和文章,尽管由于我是一个初学者,所以我不知道自己的解决方案。

当用户单击InputBox表单上的“取消”时,我需要退出sub 另外,我需要InputBox接受输入值。

    Dim UserCol As String
    Dim FirstRow As Integer

    UserCol = Application.InputBox(Prompt:="In what Column do we search? (E.g. enter: A)", Type:=2)
    If UserCol = False Then Exit Sub
' On cancel works OK. But if you type "A" (no quotes) u get a run-time ERROR 13!    

    FirstRow = Application.InputBox(Prompt:="What is your data-table's first row? (E.g. enter: 2)", Type:=1)
    If FirstRow = False Then Exit Sub
' On both cancel & input works flawlessly.

我尝试删除,Type := 2但没有任何变化。

瓦罗卡巴

您不能将字符串视为布尔值(正在做什么)。字符串可以输出正确/错误的结果,但不能输出正确的结果。尝试使用以下代码:

  Dim UserCol As String
  Dim FirstRow As Integer

  UserCol = Application.InputBox(Prompt:="In what Column do we search? (E.g. enter: A)", Type:=2)
  If Len(Trim(UserCol)) < 1 Then Exit Sub
' On cancel works OK. But if you type "A" (no quotes) u get a run-time ERROR 13!

  FirstRow = Application.InputBox(Prompt:="What is your data-table's first row? (E.g. enter: 2)", Type:=1)
  If FirstRow < 1 Then Exit Sub

Sub如果(“修剪”)输入字符串的长度小于1,则第一个条件为假(并且退出)。如果输入字符串不是数字,则第二个条件为假

注意:请记住,第二个条件不触发错误的原因是因为整数确实“支持布尔值”;尽管此处没有任何实际含义:如果删除此条件,则不会发生任何变化。我的条件检查您真正想要的(行大于或等于1)。还要记住,它InputBox支持整数,但是通常不是这种情况(对于大多数此类控件,您必须将输入作为字符串并将其转换为整数;无论是明示还是暗含)。

更新 -

可以算出“取消”按钮的点击次数:

   Dim UserCol As String
   Dim FirstRow As Integer

   UserCol = Application.InputBox(Prompt:="In what Column do we search? (E.g. enter: A)", Type:=2)

   If (LCase(UserCol) <> "false") Then

     If Len(Trim(UserCol)) < 1 Then Exit Sub
     ' On cancel works OK. But if you type "A" (no quotes) u get a run-time ERROR 13!

     FirstRow = Application.InputBox(Prompt:="What is your data-table's first row? (E.g. enter: 2)", Type:=1)
     If (FirstRow < 1) Then Exit Sub
  End If

如果第一个InputBox被取消,则返回“ False”(作为字符串),如果第二个InputBox被取消,则返回0(因此原始条件可以处理)。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

运行时错误13,类型不匹配MsgBox取消

Excel VBA运行时错误“ 13”:将变量数组传递给Application.Index方法时,类型不匹配

得到运行时错误“ 13”:运行以下宏时键入不匹配

运行时错误13:每次我运行代码时都键入不匹配

运行时错误“13”:显示来自 JSON 数组的数据时出现类型不匹配错误

工作的DLL代码失败,并出现运行时错误13:调试时类型不匹配

运行时错误'13':清除单元格内容时键入不匹配

CLng具有文本字符串时,运行时错误13不匹配

打开工作簿时如何解决“运行时错误” 13'类型不匹配

运行时错误'13:比较日期时键入不匹配-EXCEL VBA

尝试检测是否关闭了用户窗体时在Excel VBA中得到“运行时错误'13':类型不匹配”

尝试检查单元格是否包含整数时出现运行时错误 13 类型不匹配

尝试以编程方式创建用户窗体按钮时,运行时“ 13”错误类型不匹配

运行时错误 13 - 尝试编辑 Lotus Notes 的富文本项时键入不匹配

在某些值之间运行IF函数时,VBA类型不匹配/运行时错误为'13'-为什么会发生这种情况?试图隐藏空的行

运行时错误13:尝试从另一个工作簿中调用函数时,在VBA中键入不匹配

比较不同/单独表列中的值时,出现“类型不匹配”运行时错误

使用Trim时出现运行时错误13

当我尝试运行 sql 时,我遇到了运行时错误类型不匹配

Application.Wait和Excel VBA运行时错误'13'类型不匹配

运行时错误“13”:宏中的类型不匹配

Excel VBA运行时错误类型不匹配13

Excel VBA运行时错误“ 13”:类型不匹配

运行时错误'13':类型不匹配,定义了PivotCahe

运行时错误“13”类型不匹配:如果或

类型不匹配运行时错误13

运行时错误13 /类型不匹配

运行时错误 13' 类型不匹配 VBA excel

工作表运行时错误“13”:类型不匹配