简体   繁体   中英

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. 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.

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. If the output matches your expectation, then you'll have to use the Val function again, like this:

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

forcing the updateCell to become something you can subtract from.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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