【问题标题】:type mismatch error while performing division执行除法时类型不匹配错误
【发布时间】:2022-01-22 12:27:15
【问题描述】:

我正在尝试制作交易计算器,用户在框中添加值,然后将其除以他输入的值的总数,例如:50 + 60 + 70 / 3 所以,我希望除法文本框来计算框的数量在那里并选择了除法的数字,因此用户不必添加除法值,它将自动执行。我会附上样本

Dim total As Double
Dim text_percen As Double


total = 0
If Len(Trading_calculator1.txt_currency1.Value) > 0 Then total = total + Trading_calculator1.txt_currency1.Value

If Len(Trading_calculator1.txt_currency2.Value) > 0 Then total = total + Trading_calculator1.txt_currency2.Value

If Len(Trading_calculator1.txt_currency3.Value) > 0 Then total = total + Trading_calculator1.txt_currency3.Value

If Len(Trading_calculator1.txt_currency4.Value) > 0 Then total = total + Trading_calculator1.txt_currency4.Value

If Len(Trading_calculator1.txt_currency5.Value) > 0 Then total = total + Trading_calculator1.txt_currency5.Value

If Len(Trading_calculator1.txt_currency6.Value) > 0 Then total = total + Trading_calculator1.txt_currency6.Value

If Len(Trading_calculator1.txt_currency7.Value) > 0 Then total = total + Trading_calculator1.txt_currency7.Value

If Len(Trading_calculator1.txt_currency8.Value) > 0 Then total = total + Trading_calculator1.txt_currency8.Value

If Len(Trading_calculator1.txt_currency9.Value) > 0 Then total = total + Trading_calculator1.txt_currency9.Value

If Len(Trading_calculator1.txt_currency10.Value) > 0 Then total = total + Trading_calculator1.txt_currency10.Value

If Len(Trading_calculator1.txt_currency11.Value) > 0 Then total = total + Trading_calculator1.txt_currency11.Value

If Len(Trading_calculator1.txt_currency12.Value) > 0 Then total = total + Trading_calculator1.txt_currency12.Value

If Len(Trading_calculator1.txt_currency13.Value) > 0 Then total = total + Trading_calculator1.txt_currency13.Value



If Trading_calculator1.txt_divide.Value <> "" Then total = total / Trading_calculator1.txt_divide.Value

Trading_calculator1.text_percen = total

我尝试了上面的代码,但它给了我:

类型不匹配错误。

谁能帮忙?

【问题讨论】:

  • 也许试试(例如)total = total + CDbl(Trading_calculator1.txt_currency1.Value) 或者告诉我们究竟是哪一行引发了错误。
  • 代替所有重复的代码,您可以参考每个文本框,例如 For n = 1 to 13: With Me.Controls("txt_currency" &amp; n):...(假设这是您正在使用的用户表单)
  • 这里给了我错误:If Trading_calculator1.txt_divide.Value "" Then total = total / Trading_calculator1.txt_divide.Value
  • 除了检查它的非零长度之外,您还需要检查Trading_calculator1.txt_divide.Value是否为数字。
  • 如何检查?

标签: excel vba


【解决方案1】:

如果没有显式类型转换,您将依赖隐式类型转换,但并非所有字符串都可以转换为双精度值。最好有一个函数接受字符串值、处理错误并返回有效的双精度值 - 例如:

Private Function ToDouble(ByVal Value As String) As Double
    On Error GoTo ErrHandler
    ToDouble = CDbl(Value)
CleanExit:
    Exit Function
ErrHandler:
    Debug.Print "Value '" & Value & "' could not be converted and will be treated as 0"
    ToDouble = 0
    Resume CleanExit
End Function

现在您可以大大简化您的代码(Tim's advice 更是如此):

total = total + ToDouble(Trading_calculator1.txt_currency1.Value)
...

旁注,如果Trading_calculator1 是一个用户表单,并且该代码位于该表单的代码隐藏模块中,您可以通过将表单称为Me 来避免将来的麻烦,如下所示:

total = total + ToDouble(Me.txt_currency1.Value)
...

确保函数为除数返回一个非零值,否则您现在将收到 除以零 错误。

【讨论】:

  • 我尝试将其转换为双倍,就像您在 VBA 中使用“CDBL”所说的那样,当我使用“Me”时,它会说“错误使用 Me”,除此之外它仍然给我错误:如果Trading_calculator1.txt_divide.Value "" 那么总计 = 总计 / Trading_calculator1.txt_divide.Value
  • 必须验证/转换每一个您从文本框(或任何其他类型的用户输入)中读取的字符串值来自其他任何地方的用户输入,实际上) - 永远不要相信用户输入!至于Me 限定符,我在它前面加上如果Trading_calculator1 是一个用户窗体并且该代码位于该窗体的代码隐藏模块中 ;-) 如果你的代码在一个标准模块 (.bas),没有Me,因为没有当前实例 ...这使得它们成为公开宏过程和用户​​定义函数的好地方。
  • 我想我修复了它,基本上,我从模块中取出了该代码并将其添加到主 txt 框下方,在这种情况下,它将仅在该类中运行,除法框并且有效但我改变了一点,而不是直接从主代码中获取值并期望除法答案。在给出求和图的结果后,我将值除以:5+5+5= results then results/ selected value
猜你喜欢
  • 2013-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多