Excel VBA比较两个数组并创建结果数组

时间:2019-05-23 16:54:15

标签: excel vba

我有两个数组(每个数组都有三列,大约有18.000行)。我想比较array1,array2,然后创建一个结果数组。 有关更多详细信息,请转到我的xlsb文件链接。 http://www.mediafire.com/file/r38m45t2s93wy5y/compare_array-.xlsb/file

2 个答案:

答案 0 :(得分:0)

如果要逐行进行(即比较数组A的第1行与数组B的第1行),一种方法是使用VBA遍历每一行并比较值。您可以将结果输出到一个新的数组中(也许因为三个输入数组而可以将其称为数组D)。

在VBA代码中,首先确定多少行数据。

firstrow = 1
lastrow = ActiveSheet.UsedRange.Row + ActiveSheet.UsedRange.Rows.Count - 1

在以下代码中,我将假设: 数组A是1-3列 数组B是第4-6列 然后将数组D输出到第10-12列

for i = firstrow to lastrow:
   'compare array A value 1 to array B value 1
   If ActiveSheet.cells(i, 1).value == ActiveSheet.cells(i, 4).value Then 
       'Write result for array D value 1 if equal
       ActiveSheet.cells(i, 10).value = "A & B are equal"
   Else 
       'Write result for array D value 1 if unequal
       ActiveSheet.cells(i, 10).value = "A & B are NOT equal"

   'Make comparison between array A value 2 to array value 2
   If ActiveSheet.cells(i, 2).value == ActiveSheet.cells(i, 5).value Then 
       'Write result for array D value 2 if equal
       ActiveSheet.cells(i, 11).value = "A & B are equal"
   Else 
       'Write result for array D value 2 if unequal
       ActiveSheet.cells(i, 11).value = "A & B are NOT equal"    

   'Make comparison between array A value 3 to array value 3
   If ActiveSheet.cells(i, 3).value == ActiveSheet.cells(i, 6).value Then 
       'Write result for array D value 3 if equal
       ActiveSheet.cells(i, 12).value = "A & B are equal"
   Else 
       'Write result for array D value 3 if unequal
       ActiveSheet.cells(i, 12).value = "A & B are NOT equal"    
next i 

答案 1 :(得分:0)

Image of my array.

这是提取我的文件之一

Array 1	     Array 1	   Array 1     Array 2   	Array 2 	Array 2
Code 1	      INV 1	      Q1	  Code 2	    INV 2	Q2
2000000001206	NK 1801/001	40	2000000001206	NK 1801/001	40
8935001878711	NK 1801/001	40	8935001878711	NK 1801/001	40
8935001882381	NK 1801/001	20	8936120920343	NK 1801/001	10
9786040108203	NK 1801/001	5	9786040108203	NK 1801/001	5
9786049448577	NK 1801/002	3       9786049448577	NK 1801/003	3
9786049448607	NK 1801/002	3 	9786049448607	NK 1801/003	3
9786049448812	NK 1801/002	3 	9786049448812	NK 1801/003	3
2000000001429	NK 1801/004	5 	2000000001429	NK 1801/004	5
8934994208161	NK 1801/004	5 	8934994208161	NK 1801/004	5
6691188352292	NK 1801/005	12	2000000001429	NK 1801/004	5
2000000001206	NK 1802/006	1 	8934994208161	NK 1801/004	5
2000000001278	NK 1802/006	2 	2000000002268	NK 1801/005	1
2000000001485	NK 1802/006	3 	6691188352162	NK 1801/005	4
8936120920343	NK 1803/007	3 	6691188352216	NK 1801/005	2
8936120920350	NK 1803/007	5 	6691188352292	NK 1801/005	12
2000000002454	NK 1803/007	5 	2000000001206	NK 1802/006	1
2000000002455	NK 1803/008	5 	2000000001276	NK 1802/006	20
2000000002456	NK 1803/008	5 	2000000001485	NK 1802/006	5
9786040021854	NK 1903/009	3 	8936120920336	NK 1803/007	5
9786040021854	NK 1903/009	3 	8936120920343	NK 1803/007	5
9786040108203	NK 1903/009	5 	2000000002454	NK 1803/007	5
9786040108203	NK 1903/009	5 	2000000002455	NK 1803/008	1
9786040108234	NK 1903/009	5 	9786040021854	NK 1903/009	3
9786040108234	NK 1903/009	5 	9786040108203	NK 1903/009	5
2000000000987	NK 1801/010	42	9786040108234	NK 1903/009	5
2000000000988	NK 1801/010	42	2000000000987	NK 1801/010	42
2000000001527	NK 1801/010	30	2000000000988	NK 1801/010	42
2000000001990	NK 1801/010	1 	2000000001527	NK 1801/010	30
2000000001991	NK 1801/010	1 	2000000001990	NK 1801/010	1
...................more and more under...................
---------------------and here is my result array--------------
Result Array	Result Array	Result Array	Result Array
Code 1	      INV 1	      Q1	  Code 2	      INV 2	    Q2
2000000001206	NK 1801/001	40	2000000001206	NK 1801/001	40
8935001878711	NK 1801/001	40	8935001878711	NK 1801/001	40
8935001882381	NK 1801/001	20			
			                8936120920343	NK 1801/001	10
9786040108203	NK 1801/001	5  	9786040108203	NK 1801/001	5
9786049448577	NK 1801/002	3	9786049448577	NK 1801/003	3
9786049448607	NK 1801/002	3	9786049448607	NK 1801/003	3
9786049448812	NK 1801/002	3	9786049448812	NK 1801/003	3
2000000001429	NK 1801/004	5	2000000001429	NK 1801/004	5
8934994208161	NK 1801/004	5 	8934994208161	NK 1801/004	5
			                2000000001429	NK 1801/004	5
			                8934994208161	NK 1801/004	5
			                2000000002268	NK 1801/005	1
			                6691188352162	NK 1801/005	4
			                6691188352216	NK 1801/005	2
6691188352292	NK 1801/005	12	6691188352292	NK 1801/005	12
2000000001206	NK 1802/006	1	2000000001206	NK 1802/006	1
2000000001278	NK 1802/006	2			
			                2000000001276	NK 1802/006	20
2000000001485	NK 1802/006	3	2000000001485	NK 1802/006	5
			                8936120920336	NK 1803/007	5
8936120920343	NK 1803/007	3       8936120920343	NK 1803/007	5
8936120920350	NK 1803/007	5			
2000000002454	NK 1803/007	5       2000000002454	NK 1803/007	5
2000000002455	NK 1803/008	5	2000000002455	NK 1803/008	1
2000000002456	NK 1803/008	5			
9786040021854	NK 1903/009	3	9786040021854	NK 1903/009	3
9786040021854	NK 1903/009	3			
9786040108203	NK 1903/009	5 	9786040108203	NK 1903/009	5
9786040108203	NK 1903/009	5			
9786040108234	NK 1903/009	5	9786040108234	NK 1903/009	5
9786040108234	NK 1903/009	5			
2000000000987	NK 1801/010	42	2000000000987	NK 1801/010	42
2000000000988	NK 1801/010	42	2000000000988	NK 1801/010	42
2000000001527	NK 1801/010	30	2000000001527	NK 1801/010	30
2000000001990	NK 1801/010	1	2000000001990	NK 1801/010	1
2000000001991	NK 1801/010	1 	2000000001991	NK 1801/010	1
........................ more and more under...............................