Excel 为什么我会得到一个“a”;运行时错误';13';:“类型不匹配”;错误消息?

Excel 为什么我会得到一个“a”;运行时错误';13';:“类型不匹配”;错误消息?,excel,vba,runtime-error,Excel,Vba,Runtime Error,我对VBA非常陌生,仍在学习编程语言和编码。在参考了一些在线教程和线程之后,我为我的项目编写了以下代码。当我运行代码时,我会收到一条错误消息,上面写着:运行时错误:类型不匹配。如果各位专家能帮助我找到错误,我将不胜感激 提前多谢 Dim wsServiceRegistry As Worksheet, wsInhouseMaterialInventory As Worksheet Dim updateCell As Range Dim emptyRow As Long With ThisWork

我对VBA非常陌生,仍在学习编程语言和编码。在参考了一些在线教程和线程之后,我为我的项目编写了以下代码。当我运行代码时,我会收到一条错误消息,上面写着:运行时错误:类型不匹配。如果各位专家能帮助我找到错误,我将不胜感激

提前多谢

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
将wsServiceRegistry设置为工作表,将WSInHouse MaterialInventory设置为工作表
Dim updateCell As范围
暗空如长
使用此工作簿
设置wsServiceRegistry=.Worksheets(“服务注册表”)
设置wsInHouse MaterialInventory=.Worksheets(“内部材料库存”)
以
设置updateCell=wsInHouseMeterialInventory.Cells(Me.InHouseMeterialComboBox.ListIndex+2,4)
updateCell.Value=updateCell.Value-Val(Me.MaterialQuantityTextBox.Value)
如果updateCell.Value<1,则updateCell.EntireRow.Delete:退出Sub

这里唯一可能导致类型不匹配的行是
updateCell.Value=updateCell.Value-Val(Me.MaterialQuantityTextBox.Value)
,这取决于单元格包含的内容

首先要确定的是,它是否是您真正想要的单元格,以及它是否包含您期望的内容。将这些行添加到相关行的正上方:

Debug.Print updateCell.Address
Debug.Print updateCell.Value
按Ctrl+G以显示调试输出并运行它。如果输出符合您的期望,则必须再次使用
Val
函数,如下所示:

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

强制
updateCell
成为您可以从中减去的内容。

您应该声明
updateCell
范围
,而不是
。感谢Rory抽出宝贵的时间来帮助我。现在,编译错误问题已经解决了,但我得到一个错误说“运行时错误'13'类型不匹配。如果您能帮助我消除此错误,我们将不胜感激。谢谢我按照建议更改了代码。错误是由哪一行引起的?updateCell的当前值是多少?
MaterialQuantityTextBox
中的文本是什么?