VB 二维数组对比 将对比值放入另一个二维数组中
有这么2个数据 arr1(2,1) arr2(3,1)
arr1数组中存放的值是这样:
arr1(0,0)=张三 arr1(0,1)=182
arr1(1,0)=李四 arr1(1,1)=188
arr1(2,0)=王五 arr1(2,1)=184
arr2数组中存放的值是这样:
arr2(0,0)=李四 arr2(0,1)=182
arr2(1,0)=张三 arr2(1,1)=152
arr2(2,0)=赵六 arr2(2,1)=199
arr2(3,0)=王五 arr2(3,1)=200
-----------------------------------------------
需求是这样的,在ARR2数组中找与arr1数组中名字相同的 取出各自对应的值,然后算差值用arr1的值减去ARR2的值
如果arr1中的名称在ARR2中没有找到则去ARR2的值
即最终的结果应该是
arr3(0,0)=张三 arr2(0,1)= 30
arr3(1,0)=李四 arr2(1,1)= 6
arr3(2,0)=王五 arr2(2,1)= -16
arr3(3,0)=赵六 arr2(3,1)= -199
这个问题怎么解决啊 高手帮帮!
VB怎么实现?小弟在线等
[解决办法]
方法很多,给一个使用 ListBox 的例子(均设置为不可见):假设你的数组是 Variant 型。
Private Declare Function SendMessagebyString Lib _
"user32" Alias "SendMessageA" (ByVal hWND As Long, _
ByVal wMsg As Long, ByVal wParam As Long, _
ByVal lParam As String) As Long
Private Const LB_FINDSTRINGEXACT = &H1A2 '在 ListBox 中精确查找
Private Sub Command1_Click()
Dim i As Long, n As Long
List1.Clear
List2.Clear
For i = LBound(arr1, 1) To UBound(arr1, 1)
List1.AddItem arr1(i, 0)
List1.ItemData(List1.NewIndex) = arr1(i, 1)
Next i
For i = LBound(arr2, 1) To UBound(arr2, 1)
n = SendMessagebyString(List1.hWnd, LB_FINDSTRINGEXACT, -1, arr2(i, 0))
List2.AddItem arr2(i, 0)
If n = -1 Then
List2.ItemData(list2.NewIndex) = 0 - arr2(arr(i, i)
Else
List2.ItemData(list2.NewIndex) = List1.ItemData(n) - arr2(arr(i, i)
End If
Next i
Redim arr3(List2.ListCount - 1)
For i = 0 To List2.ListCount - 1
arr3(i, 0) = List2.List(i)
arr3(i, 1) = List2.ItemData(i)
Next i