我需要创建一个由符合特定标准的行号组成的数组。
假设我有一份日期清单:
01.03.2019
01.07.2019
01.03.2020
30.03.2019
01.03.2019
01.06.2019
现在,假设我需要01.03.2019的行号,我的数组将是1,5。
有什么像样的方法吗?我的列表有数千个条目,所以我想循环遍历每个单元需要一段时间。任何精益解决方案都将不胜感激。
我找到了这个线程,但是答案还没有被验证:Is it possible to fill an array with row numbers which match a certain criteria without looping?
发布于 2019-01-08 19:56:14
如果您使用数组而不是工作表工作,则不会“花一段时间”。
dim dt as long, i as long, j as long, tmp as variant, arr as variant
dt = dateserial(2019, 1, 3)
with worksheets("sheet1")
tmp = .range(.cells(1, "A"), .cells(.rows.count, "A").end(xlup)).value2
end with
redim arr(0)
for i=lbound(tmp, 1) to ubound(tmp, 1)
if tmp(i, 1) = dt then
redim preserve arr(j)
arr(j) = i 'collect row numbers
j=j+1
end if
next i
for i=lbound(arr) to ubound(arr)
debug.print arr(i) 'print row numbers
next i发布于 2019-01-08 19:56:04
如果您的值位于A列中,这将用匹配的行号填充数组,并将它们打印到即时窗口:
Sub FillArray()
Dim myarray As Variant
Dim i As Long
ReDim myarray(0 To 0)
For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row
If Cells(i, 1).Value = "01.03.2019" Then 'Value to find
ReDim Preserve myarray(0 To UBound(myarray) + 1)
myarray(UBound(myarray) - 1) = i
End If
Next i
For i = 0 To UBound(myarray)
Debug.Print myarray(i)
Next i
End Subhttps://stackoverflow.com/questions/54098605
复制相似问题