簡體   English   中英

VBA運行時錯誤'13':類型不匹配

[英]VBA Runtime Error '13': Type Mismatch

我收到運行時錯誤'13':嘗試運行代碼時鍵入不匹配。 Debug突出顯示了'IF'和'ElseIF'語句,但是我不知道錯誤在哪里。 任何幫助,將不勝感激。 謝謝

Dim lColumn As Long
lColumn = ws.Cells(2, Columns.Count).End(xlToLeft).Column

Dim rgMonth As Range
Dim rgTaxExp As Range

Dim i As Long, j As Long
Set rgTaxExp = Range(Cells(lRow, 10), Cells(lRow, lColumn))

Set rgMonth = Range(Cells(2, 10), Cells(2, lColumn))
For i = 1 To rgMonth.Rows.Count
For j = 1 To rgMonth.Columns.Count

If Month(date2) >= Month(rgMonth.Cells(i, j).Value) Then 'Runtime Error '13':_
Type Mismatch
Cells(lRow, 9).Copy rgTaxExp.Cells(i, j)
ElseIf Month(date2) < Month(rgMonth.Cells(i, j).Value) Then 'Runtime Error '13':_
Type Mismatch
rgTaxExp.Cells(i, j) = 0

如錯誤消息所述, Month(date2)Month(rgMonth.Cells(i, j).Value)在循環中的某個時刻失敗。

在導致錯誤的If語句之前插入兩個調試語句:

For j = 1 To rgMonth.Columns.Count

Debug.Print "date2 is " & date2
Debug.Print "rgMonth.Cells(i, j).Value is " & rgMonth.Cells(i, j).Value

If Month(date2) >= Month(rgMonth.Cells(i, j).Value) Then 'Runtime Error '13':_
Type Mismatch

運行您的代碼。 當您發現錯誤時,請調試並查看立即窗口。 最后兩行應向您顯示錯誤發生的原因。

如果沒有看到立即窗口,則可以通過在Visual Basic編輯器中選擇“查看->立即窗口”來打開它。

暫無
暫無

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

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