[英]Excel VBA runtime error '13' type mismatch
Sub compareLines()
Application.ScreenUpdating = False
ActiveSheet.Cells(3, 3).Activate
While ActiveCell.Value <> ""
If ActiveCell.Value - ActiveCell.Offset(-1, 0).Value < 0 Then
ActiveCell.EntireRow.Delete
Else
ActiveCell.Offset(1, 0).Activate
End If
Wend
Application.ScreenUpdating = True
End Sub
這是我目前的錯誤代碼。
錯誤是Excel VBA runtime error "13 type mismatch
。
錯誤所在的行: If ActiveCell.Value - ActiveCell.Offset(-1, 0).Value < 0 Then
這段代碼已經在以前的工作表上工作了,但是當我將此宏導入到新的工作表上時,它似乎不起作用。 我在SO上找到的所有解決方案似乎都不適用於我的情況,因此,對此問題的任何幫助將不勝感激。
通常,您應該非常小心“ On Error Resume Next
。
如果將其放入代碼中,它將開始忽略錯誤,並且如果有人在您之后使用該代碼,他將根本不高興(或者他會認為您是業余愛好者或求職者)。 話雖如此,CPearson有一個關於它的好文章,這是值得閱讀- http://www.cpearson.com/excel/errorhandling.htm
最后但並非最不重要的一點是,一旦意識到錯誤發生的原因,請確保將On Error Resume Next
更改為其他內容。
在您的情況下,最好使用IsNumeric
函數,以避免TypeMismatch錯誤。 如果出現其他錯誤,請嘗試避免此類錯誤。
Dim v1 as Range, v2 as Range
While ActiveCell.Value <> ""
Set v1 = ActiveCell.Value
Set v2 = ActiveCell.Offset(-1)
If IsNumeric(v1) And IsNumeric(v2) Then
'Both are numeric, so it's safe to perform arithmetic against these values
If v1 - v2 < 0 Then
ActiveCell.EntireRow.Delete
Else
ActiveCell.Offset(1, 0).Activate
End If
Else: ActiveCell.Offset(1, 0).Activate
End If
Wend
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.