excel的vba 代码

看看所有的彩票是否有重复的 如果有就将结果 输入到后面的单元格 程序如下
Sub 是否有重复中奖()
Dim i As Integer
Dim t As Integer
Dim a As Integer
For i = 2 To 1191
For t = 3 To 1192
If Cells(i, 2).Value + Cells(i, 3).Value = Cells(t, 2).Value + Cells(t, 3).Value Then
a = a + 1
Cells(i, 5) = Cells(i, 2)
Cells(i, 6) = Cells(t, 3)
End If
Next
Next
MsgBox "重复中奖的号码个数为:" & a
End Sub
以上程序执行后 仿佛if then 语句没有起左右 if 后面的条件变量 不成立他也执行语句 没有跳转到end if 之后 执行next 循环 (应该只有重复的才写入E和F列,可是怎么都写入了。。。?)求教 如和修改

第1个回答  2011-03-13
看你数据还比较多,用数组给你写一段,这样运行速度会快些。而且你数据有增加或减少也一样通用。不管你是几行数据。
也不知道是否合你意,最好把你的表发过来看下是什么结构。
Sub 是否有重复中奖()
Dim i&, R&, g&, x&
Dim arr, arr1()
With Sheets("sheet1")
R = .Range("B65536").End(xlUp).Row
arr = .Range("B2:C" & R).Value
For i = 1 To UBound(arr) - 1
If arr(i, 1) = arr(i + 1, 1) And arr(i, 2) = arr(i + 1, 2) Then
g = g + 1
ReDim Preserve arr1(1 To 2, 1 To g)
For x = 1 To 2
arr1(x, g) = arr(i, x)
Next x
End If
Next i
.Range("E2:F" & .Range("E65536").End(xlUp).Row).ClearContents
.Range("E2").Resize(UBound(arr1, 2), 2) = Application.Transpose(arr1)
End With
MsgBox "重复中奖的号码个数为:" & g
End Sub这里指定为sheet1表,你表名不同可直接修改成你的存放数据的工作表名本回答被提问者采纳
第2个回答  2011-03-12
应该是你的条件不对,试试这个,
Sub 是否有重复中奖()
Dim i&, t&, a&
For i = 2 To 1191
For t = i+1 To 1192
If Cells(i, 2).Value & Cells(i, 3).Value = Cells(t, 2).Value & Cells(t, 3).Value Then
a = a + 1
Cells(i, 5) = Cells(i, 2)
Cells(i, 6) = Cells(t, 3)
End If
Next t, i
MsgBox "重复中奖的号码个数为:" & a
End Sub
第3个回答  2011-03-12
把 for t = 3 to 1192
改为 for t = i + 1 to 1192
相似回答