简体   繁体   中英

Excel VBA - matching value from array to array

I'm comparing 2 array. 1 is dynamic array and 1 is static array, both are 1-Dimension array with different size. 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.

Right now, I think that something is wrong with my condition as it gets all the values from the excel rows.

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 .

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. The i loop that you had loads up all of the daysleft. Having the x loop was causing you the match the same value in the interval more than once.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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