简体   繁体   English

为什么我会收到“运行时错误 '13':类型不匹配”错误消息?

[英]Why am I getting a" Run time Error '13': Type Mismatch" error message?

I am very new to VBA and still learning programming languages and coding.我对 VBA 很陌生,仍在学习编程语言和编码。 I came up with the following code for my project after reffering several online tutorials and threads.在参考了几个在线教程和线程后,我为我的项目想出了以下代码。 When I run the code I get an error message saying,Run-time Error: Type mismatch.If anyone of you experts could help me to find the error, it would be highly appreciated.当我运行代码时,我收到一条错误消息,说,运行时错误:类型不匹配。如果你们中的任何一位专家可以帮助我找到错误,我们将不胜感激。

Thanks a lot in advance非常感谢提前

Dim wsServiceRegistry As Worksheet, wsInhouseMaterialInventory As Worksheet
Dim updateCell As Range
Dim emptyRow As Long

With ThisWorkbook
    Set wsServiceRegistry = .Worksheets("Service Registry")
    Set wsInhouseMaterialInventory = .Worksheets("Inhouse Material Inventory")
End With

Set updateCell = wsInhouseMaterialInventory.Cells(Me.InhouseMaterialComboBox.ListIndex + 2, 4)
updateCell.Value = updateCell.Value - Val(Me.MaterialQuantityTextBox.Value)


If updateCell.Value < 1 Then updateCell.EntireRow.Delete: Exit Sub

The only line that can cause a type mismatch here is唯一可能导致类型不匹配的行是
updateCell.Value = updateCell.Value - Val(Me.MaterialQuantityTextBox.Value) , depending on what the cell contains. updateCell.Value = updateCell.Value - Val(Me.MaterialQuantityTextBox.Value) ,取决于单元格包含的内容。

The first thing to find out, is if it is the cell you actually want and if it contains what you expect.首先要找出它是否是您真正想要的单元格,以及它是否包含您期望的内容。 Add these lines just above the line in question:在相关行的正上方添加这些行:

Debug.Print updateCell.Address
Debug.Print updateCell.Value

Press Ctrl + G to show the debug output and run it.Ctrl + G显示调试输出并运行它。 If the output matches your expectation, then you'll have to use the Val function again, like this:如果输出符合您的期望,那么您将不得不再次使用Val函数,如下所示:

updateCell.Value = Val(updateCell.Value) - Val(Me.MaterialQuantityTextBox.Value)

forcing the updateCell to become something you can subtract from.强制updateCell成为您可以从中减去的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM