我正在比较2个阵列。 1是动态数组,1是静态数组,都是具有不同大小的1维数组。条件是如果动态数组中的值与静态数组中的值匹配,那么它将采用单元格值并且会发生一些其他操作。
示例案例:
dynArr = (123,123,45,23,56,90,34,Nil) ' size based on row counts
staArr = (90,60,30,0)
因此,如果dynArr
中的任何值与staArr
匹配,则会发生一些事情。
现在,我认为我的情况有问题,因为它从excel行中获取所有值。
我尝试使用application.match()
,但使用了来自msdn的说明,它似乎不起作用,就速度而言,从some other posts开始慢得多。
如何更改条件以匹配数组?或者有解决方法吗?
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
答案 0 :(得分:1)
行。我想这就是你想要的。我注释掉For x循环。你把所有的日子都加载起来的i循环。 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