Reputation: 1
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
Upvotes: 0
Views: 8929
Reputation: 22358
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.
Upvotes: 1