簡體   English   中英

為什么我會收到“運行時錯誤 '13':類型不匹配”錯誤消息?

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

我對 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

唯一可能導致類型不匹配的行是
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成為您可以從中減去的東西。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM