VB将两个数组进行对比并输出其中相同的数据
2个回答
展开全部
专门帮你做了如下 程序,并经调试通过。加分啊!
在程序中增加命令按钮一个,MSHFlexGrid四个,程序如下,
Option Explicit
Dim a(1 To 2000) As Single
Dim b(1 To 2000) As Single
Private Sub Command1_Click()
Dim i, j As Integer
With MSHFlexGrid1
.Rows = 1 + UBound(a)
.Cols = 2
For i = 1 To UBound(a)
.TextMatrix(i, 1) = a(i)
Next
.Col = 1
.Sort = 1 '排序
End With
Call RemoveduplicateRows(MSHFlexGrid1) '删除重复的行
With MSHFlexGrid2
.Rows = 1 + UBound(b)
.Cols = 2
For i = 1 To UBound(b)
.TextMatrix(i, 1) = b(i)
Next
.Col = 1
.Sort = 1 '排序
End With
Call RemoveduplicateRows(MSHFlexGrid2) '删除重复的行
With MSHFlexGrid3
.Rows = MSHFlexGrid2.Rows + MSHFlexGrid1.Rows - 1
.Cols = 2
For i = 1 To MSHFlexGrid1.Rows - 1 ''将前两表的数据合并到第三表
.TextMatrix(i, 1) = MSHFlexGrid1.TextMatrix(i, 1)
Next
For i = 1 To MSHFlexGrid2.Rows - 1
.TextMatrix(MSHFlexGrid1.Rows - 1 + i, 1) = MSHFlexGrid2.TextMatrix(i, 1)
Next
.Col = 1
.Sort = 1 '排序
j = 1
MSHFlexGrid4.Rows = 2
MSHFlexGrid4.Cols = 2
For i = 2 To .Rows - 1
If .TextMatrix(i, 1) = .TextMatrix(i - 1, 1) Then
MSHFlexGrid4.TextMatrix(MSHFlexGrid4.Rows - 1, 1) = .TextMatrix(i, 1) '前第三表中重复的数据列在第四表,此第四表中第1列的数据就是你要的答案
MSHFlexGrid4.Rows = MSHFlexGrid4.Rows + 1
End If
Next
MSHFlexGrid4.Rows = MSHFlexGrid4.Rows - 1
End With
End Sub
Private Sub RemoveduplicateRows(MSH As MSHFlexGrid) '删除重复的行
Dim i, j As Integer
With MSH
i = 1
Do
j = i
If .Rows > 2 Then
Do While j < .Rows - 1
If (.TextMatrix(i, 1) = .TextMatrix(j + 1, 1)) Or VBA.Len(.TextMatrix(j + 1, 1)) = 0 Then '删除重复的行
.RemoveItem (j + 1)
.Refresh
j = j - 1
End If
j = j + 1
Loop
End If
i = i + 1
Loop Until i >= .Rows
End With
End Sub
Private Sub Form_Load()
Dim i As Integer
Randomize
For i = 1 To 2000
a(i) = Val(VBA.Format((Rnd(i) * 100), "#.##")) 'a(1 To 2000), b(1 To 2000)是你已经有的数据,这里由随机数代替
b(i) = Val(VBA.Format((Rnd(i) * 100), "#.##"))
Next
End Sub
在程序中增加命令按钮一个,MSHFlexGrid四个,程序如下,
Option Explicit
Dim a(1 To 2000) As Single
Dim b(1 To 2000) As Single
Private Sub Command1_Click()
Dim i, j As Integer
With MSHFlexGrid1
.Rows = 1 + UBound(a)
.Cols = 2
For i = 1 To UBound(a)
.TextMatrix(i, 1) = a(i)
Next
.Col = 1
.Sort = 1 '排序
End With
Call RemoveduplicateRows(MSHFlexGrid1) '删除重复的行
With MSHFlexGrid2
.Rows = 1 + UBound(b)
.Cols = 2
For i = 1 To UBound(b)
.TextMatrix(i, 1) = b(i)
Next
.Col = 1
.Sort = 1 '排序
End With
Call RemoveduplicateRows(MSHFlexGrid2) '删除重复的行
With MSHFlexGrid3
.Rows = MSHFlexGrid2.Rows + MSHFlexGrid1.Rows - 1
.Cols = 2
For i = 1 To MSHFlexGrid1.Rows - 1 ''将前两表的数据合并到第三表
.TextMatrix(i, 1) = MSHFlexGrid1.TextMatrix(i, 1)
Next
For i = 1 To MSHFlexGrid2.Rows - 1
.TextMatrix(MSHFlexGrid1.Rows - 1 + i, 1) = MSHFlexGrid2.TextMatrix(i, 1)
Next
.Col = 1
.Sort = 1 '排序
j = 1
MSHFlexGrid4.Rows = 2
MSHFlexGrid4.Cols = 2
For i = 2 To .Rows - 1
If .TextMatrix(i, 1) = .TextMatrix(i - 1, 1) Then
MSHFlexGrid4.TextMatrix(MSHFlexGrid4.Rows - 1, 1) = .TextMatrix(i, 1) '前第三表中重复的数据列在第四表,此第四表中第1列的数据就是你要的答案
MSHFlexGrid4.Rows = MSHFlexGrid4.Rows + 1
End If
Next
MSHFlexGrid4.Rows = MSHFlexGrid4.Rows - 1
End With
End Sub
Private Sub RemoveduplicateRows(MSH As MSHFlexGrid) '删除重复的行
Dim i, j As Integer
With MSH
i = 1
Do
j = i
If .Rows > 2 Then
Do While j < .Rows - 1
If (.TextMatrix(i, 1) = .TextMatrix(j + 1, 1)) Or VBA.Len(.TextMatrix(j + 1, 1)) = 0 Then '删除重复的行
.RemoveItem (j + 1)
.Refresh
j = j - 1
End If
j = j + 1
Loop
End If
i = i + 1
Loop Until i >= .Rows
End With
End Sub
Private Sub Form_Load()
Dim i As Integer
Randomize
For i = 1 To 2000
a(i) = Val(VBA.Format((Rnd(i) * 100), "#.##")) 'a(1 To 2000), b(1 To 2000)是你已经有的数据,这里由随机数代替
b(i) = Val(VBA.Format((Rnd(i) * 100), "#.##"))
Next
End Sub
2014-03-09
展开全部
是无序对比还是有序对比??
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询