VBA 运行时错误“13”:类型不匹配
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17368154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA Runtime Error '13': Type Mismatch
提问by user2532363
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
我在尝试运行代码时收到运行时错误“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
回答by Jon Crowell
As the error message states, either Month(date2)
or Month(rgMonth.Cells(i, j).Value)
is failing at some point in your loop.
由于错误消息指出,无论是Month(date2)
或Month(rgMonth.Cells(i, j).Value)
在你的循环某些点失败。
Insert two debug statements before the If statement that is causing the error:
在导致错误的 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
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.
运行你的代码。当您遇到错误时,请调试并查看“立即”窗口。最后 2 行应显示发生错误的原因。
If you don't see the Immediate window, you can open it by selecting View --> Immediate Window from within the Visual Basic Editor.
如果您没有看到立即窗口,您可以通过从 Visual Basic 编辑器中选择查看 --> 立即窗口来打开它。