类型不匹配错误,为什么?
问题描述:
任何人都可以解释为什么有类型不匹配如果范围...运行?
这里是我的代码类型不匹配错误,为什么?
Sub Button2_Click()
Dim i As Integer
Dim k As Integer
For i = 2 To 1000
For x = 3 To 999
If Range("k" & i & ":bn" & i).Value = Range("k" & x & ":bn" & x).Value And Cells(i, 5).Value <> Cells(x, 5).Value Then
Range("k" & i & ":bn" & i).Interior.ColorIndex = 7
Range("k" & x & ":bn" & x).Interior.ColorIndex = 7
End If
Next x
Next i
End Sub
我试图用Cstr()
,但什么都没有改变
UP:我试图用一个多循环和细胞的替代范围,只有我拿到的是应用程序定义或对象定义的错误是:
Dim z As Integer
...
For z = 6 To 30
If Cells(i, z).Value = Cells(x, z).Value And Cells(i, 5).Value <> Cells(x, 5).Value Then
TNX提前
答
的问题是,你是TR以比较相同“形状”但多于一个单元格的两个范围。 Excel的VBA不允许this..something像:
Sub test1()
Dim r1 As Range, r2 As Range
Set r1 = Range("A1:A2")
Set r2 = Range("B1:B2")
If r1.Value = r2.Value Then
MsgBox "same"
End If
End Sub
将失败.............你需要的元素逐元素的比较,如:
Sub test2()
Dim r1 As Range, r2 As Range
Set r1 = Range("A1:A2")
Set r2 = Range("B1:B2")
Dim i As Long, b As Boolean
b = True
For i = 1 To 2
If r2(i).Value <> r1(i).Value Then
b = False
End If
Next i
If b Then
MsgBox "same"
End If
End Sub
你不能像这样比较整个范围:'Range(“K2:BN2”)。Value = Range(“K3:BN3”)。Value。看到这个问题如何做到这一点:http://stackoverflow.com/questions/19395633/how-to-compare-two-entire-rows-in-a-sheet/19396257#19396257 –
@simoco我可以使用单元格( ).value代替? – Seya
您可以只比较_one_ cell与_another_ cell。但是你不能以这种方式比较_entire ranges_。如果您需要比较整个范围 - 请参阅上面给出的链接 –