简体   繁体   English

Excel VBA-在数组之间匹配值

[英]Excel VBA - matching value from array to array

I'm comparing 2 array. 我正在比较2数组。 1 is dynamic array and 1 is static array, both are 1-Dimension array with different size. 1是动态数组,1是静态数组,都是大小不同的一维数组。 The condition is that if the value in the dynamic array matches to the value in the static array, then it would take cell values and some other action would happens. 条件是,如果动态数组中的值与静态数组中的值匹配,则它将采用单元格值,并且将发生其他操作。

example case: 示例案例:

dynArr = (123,123,45,23,56,90,34,Nil) ' size based on row counts
staArr = (90,60,30,0)

So if any value in dynArr matches to staArr , some thing happens. 因此,如果dynArr中的任何值与dynArr匹配, staArr发生某些情况。

Right now, I think that something is wrong with my condition as it gets all the values from the excel rows. 现在,我认为我的情况出了问题,因为它从excel行中获取了所有值。

I tried used application.match() but with description from msdn , it does not seem to work and in terms of speed wise, it much more slower from some other posts . 我尝试使用application.match()但从msdn的描述来看,它似乎不起作用,并且就速度而言,它比其他 一些 文章要慢得多。

How should I change my condition to match between arrays? 如何更改条件以在阵列之间匹配? or is there any workaround? 或有什么解决方法?

Sub getValue()

'define dynamic array
Dim sn, sb, interval, daysleft As Variant
'define static array
interval = Array(90, 60, 30, 14, 7, 0, -2)

Dim cnt As Long
Dim i, x, y As Long

ReDim sn(1 To 1)
ReDim sb(1 To 1)
ReDim daysleft(1 To 1)

cnt = 0

'Loop through all the row
For i = 1 To Cells(Rows.Count, "L").End(xlUp).Row
    'redim daysleft based on the rows
    ReDim Preserve daysleft(1 To i)
    daysleft(i) = Cells(i, "L").Value

    For x = LBound(daysleft) To UBound(daysleft)
     For y = LBound(interval) To UBound(interval)

        If daysleft(x) = interval(y) Then 'Loops everything

            'assign cell value to array
            cnt = cnt + 1
            ReDim Preserve sn(1 To cnt)
            ReDim Preserve sb(1 To cnt)

            sn(cnt) = Cells(i, "A").Value
            sb(cnt) = Cells(i, "F").Value

         End If
        Next y
    Next x
Next i

For i = 1 To (cnt - 1)
    Debug.Print "This is sn: "; sn(i) 'gets all the value instead of the matching case
  'Call test(sn(i), sb(i))
Next

End Sub

OK. 好。 I think this is what you want. 我想这就是你想要的。 I commented out the For x loop. 我注释掉了For x循环。 The i loop that you had loads up all of the daysleft. 您拥有的i循环将剩余所有天数加载起来。 Having the x loop was causing you the match the same value in the interval more than once. 使用x循环会导致您在间隔中多次匹配相同的值。

Sub getValue()

    'define dynamic array
    Dim sn, sb, interval, daysleft As Variant
    'define static array
    interval = Array(90, 60, 30, 14, 7, 0, -2)

    Dim cnt As Long
    Dim i, x, y As Long

    ReDim sn(1 To 1)
    ReDim sb(1 To 1)
    ReDim daysleft(1 To 1)

    cnt = 0

    ' Loop through all the row
    For i = 1 To Cells(Rows.Count, "L").End(xlUp).row
        'redim daysleft based on the rows
        ReDim Preserve daysleft(1 To i)
        daysleft(i) = Cells(i, "L").Value

        'For x = LBound(daysleft) To UBound(daysleft)
            For y = LBound(interval) To UBound(interval)

               If daysleft(i) = interval(y) Then 'Loops everything

               'assign cell value to array
               cnt = cnt + 1
               ReDim Preserve sn(1 To cnt)
               ReDim Preserve sb(1 To cnt)

               sn(cnt) = Cells(i, "A").Value
               sb(cnt) = Cells(i, "F").Value

            End If
            Next y
         'Next x
     Next i

     For i = 1 To (cnt)
          Debug.Print "This is sn: "; sn(i) 'gets all the value instead of the matching case
         'Call test(sn(i), sb(i))
     Next

End Sub

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

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