VBA运行时错误'13':类型不匹配

时间:2013-06-28 15:18:37

标签: excel-vba vba excel

我收到运行时错误'13':当我尝试运行代码时键入不匹配。调试突出显示'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

1 个答案:

答案 0 :(得分:1)

正如错误消息所述,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

运行您的代码。当您遇到错误时,请调试并查看立即窗口。最后两行应该显示错误发生的原因。

如果您没有看到立即窗口,可以通过选择查看 - &gt;打开它。 Visual Basic编辑器中的立即窗口。