简体   繁体   English

vba 循环遍历数组,将值存储到 arrayi

[英]vba loop through array, store values to arrayi

I have some data, stored in arrays like我有一些数据,存储在数组中

Dim arrA, arrB, arrC, arrAi, arrBi
Dim i as integer, x as integer

for i = 1 to 100

    if cells(i,1).value = "criteria" then ' just add value to array when it meets some criteria
        x = x + 1
        arrA(x) = cells(i,1).value
        arrB(x) = cells(i,2).value
        arrC(x) = cells(i,3).value
    end if

next i

redim preserve arrA(1 to x)
redim preserve arrB(1 to x)
redim preserve arrC(1 to x)

And the data looks like数据看起来像

arrA: 26.1 40.2 80.3 26.0 41.3 78.7 25.8 40.8 80.0 arrA:26.1 40.2 80.3 26.0 41.3 78.7 25.8 40.8 80.0

arrB: 10 11 10 66 67 64 32 32 33 arrB: 10 11 10 66 67 64 32 32 33

arrC: 1 2 3 1 2 3 1 2 3 arrC: 1 2 3 1 2 3 1 2 3

Since the values in arrA 26.1, 26.0, 25.8 (position 1, 4, 7) belong to group 1 (referencing to values in arrC at same position), I would like to store 26.1 26.0 25.8 to arrAi and 10 66 32 to arrBi for subsequent calculations.由于 arrA 26.1、26.0、25.8(位置 1、4、7)中的值属于第 1 组(参考 arrC 中相同位置的值),我想将 26.1 26.0 25.8 存储到 arrAi 和 10 66 32 到 arrBi后续计算。

How can I loop through the 3 arrays and store values to another array as described above?如上所述,如何遍历 3 个数组并将值存储到另一个数组?

Thanks in advance.提前致谢。

Try the next way, please:请尝试下一种方法:

Sub handleArraysFromArrays()
 'your existing code...
 'but you fistly must declare
  Dim arrA(1 To 100), arrB(1 To 100), arrC(1 To 100)
 '....
 'your existing code
 '...
 Dim k As Long, kk As Long
 
 ReDim arrAi(1 To UBound(arrA))
 ReDim arrBi(1 To UBound(arrA))
 For i = 1 To UBound(arrC)
    If arrC(i, 1) = 1 Then k = k + 1: arrAi(k, 1) = arrA(i, 1)
    If arrC(i, 1) = 2 Then kk = kk + 1: arrBi(kk, 1) = arrA(i, 1)
 Next i
 ReDim Preserve arrAi(1 To k): ReDim Preserve arrBi(1 To kk)
 Debug.Print UBound(arrAi), UBound(arrBi)
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM