简体   繁体   中英

VBA Runtime Error '13': Type Mismatch

I receive Runtime Error '13': Type Mismatch when I try to run the code. Debug highlights the 'IF' and 'ElseIF' statements, but I can't figure out where the mistake is. Any help would be appreciated. Thanks

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

As the error message states, either Month(date2) or Month(rgMonth.Cells(i, j).Value) is failing at some point in your loop.

Insert two debug statements before the If statement that is causing the error:

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

Run your code. When you get to the error, debug and take a look at the Immediate window. The last 2 lines should show you why the error is occurring.

If you don't see the Immediate window, you can open it by selecting View --> Immediate Window from within the Visual Basic Editor.

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