0

我在尝试运行代码时收到运行时错误“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
4

1 回答 1

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

运行你的代码。当您遇到错误时,调试并查看立即窗口。最后两行应该告诉你为什么会发生错误。

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

于 2013-06-28T21:08:04.317 回答