gpt4 book ai didi

vba - For 循环中的 If 语句中的类型不匹配

转载 作者:行者123 更新时间:2023-12-04 21:37:44 24 4
gpt4 key购买 nike

我在试图弄清楚为什么下面的代码给我一个 Error '13' 时遇到了很多麻烦。 .难道我做错了什么?

Sub summary()
Dim last As Variant
lastrow = Sheet4.Range("g" & Rows.Count).End(xlUp).Row
c = 0
For x = 2 To lastrow
If Sheet4.Cells(x, 10) = Sheet4.Cells(x, 12) Then
c = c + 1
End If
Next x
End Sub

最佳答案

检查 Col 10 或 Col 12 中的一个单元格。它们有公式错误。 #NA , #DIV/0!或其他东西,因此也许这就是你得到 Run Time Error 13 Type Mismatch Error 的原因.

检查哪个单元格的最佳方法是找到 x 的值在错误的时候。

这是一个复制问题的示例

=0/0在单元格中 A1并运行此代码。

enter image description here

编辑 要找出问题可能在哪一行,试试这个简单的事情。

Sub summary()
Dim lastrow As Long
Dim c As Long, x As Long

On Error GoTo Whoa

With Sheet4
lastrow = .Range("g" & .Rows.Count).End(xlUp).Row

c = 0
For x = 2 To lastrow
If .Cells(x, 10) = .Cells(x, 12) Then
c = c + 1
End If
Next x
End With

Exit Sub
Whoa:
MsgBox "At the time of error the value of x is " & x
End Sub

关于vba - For 循环中的 If 语句中的类型不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30383577/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com