Excel 过滤并复制粘贴VBA值

Excel 过滤并复制粘贴VBA值,excel,vba,Excel,Vba,对于我前面的问题,我一直在尝试提取两个日期之间的Col“A”值。我通过Excel公式得到了解决方案,但我不知道,对于大数据,公式将花费很多时间来处理结果 =AGGREGATE(14,6,A:A*(B:B>=$F$2)*(B:B<=$G$2),ROW(A1)) 在VBA中工作时,学习使用数组将使您受益匪浅,因为在内存中工作将使脚本的速度成倍提高 为了让您走上正轨,这应该有助于您找到答案: Option Explicit 'always add this Su

对于我前面的问题,我一直在尝试提取两个日期之间的Col“A”值。我通过Excel公式得到了解决方案,但我不知道,对于大数据,公式将花费很多时间来处理结果

=AGGREGATE(14,6,A:A*(B:B>=$F$2)*(B:B<=$G$2),ROW(A1))

在VBA中工作时,学习使用数组将使您受益匪浅,因为在内存中工作将使脚本的速度成倍提高

为了让您走上正轨,这应该有助于您找到答案:

    Option Explicit 'always add this
    
    Sub test()
        Dim arr, arr2, j As Long, i As Long, Dstart As Date, Dend As Date
        
        arr = Sheet1.Range("A1").CurrentRegion.Value 'get the dates in memory
        Dstart = Sheet1.Range("c1").Value
        Dend = Sheet1.Range("c2").Value
        ReDim arr2(1 To UBound(arr), 1 To 1) 'setup temp array
        i = 1 'don't like to start at 0
        For j = 1 To UBound(arr) 'traverse source dates
            If arr(j, 1) >= Dstart And arr(j, 1) <= Dend Then 'check if date is in range
                arr2(i, 1) = arr(j, 1) 'store matching dates in temp array
                i = i + 1
            End If
        Next j
        
        With Sheet2
            .Range(.Cells(1, 1), .Cells(UBound(arr2), 1)).Value = arr2 'paste to sheet
        End With
    End Sub
Option Explicit'始终添加此选项
子测试()
Dim arr,arr2,j等于长,i等于长,Dstart等于日期,Dend等于日期
arr=Sheet1.Range(“A1”).CurrentRegion.Value'获取内存中的日期
Dstart=Sheet1.范围(“c1”).值
Dend=表1.范围(“c2”).值
ReDim arr2(1对UBound(arr),1对1)“设置临时阵列
i=1'不喜欢从0开始
对于j=1到uBond(arr)'遍历源日期
如果arr(j,1)>=Dstart和arr(j,1)
    Option Explicit 'always add this
    
    Sub test()
        Dim arr, arr2, j As Long, i As Long, Dstart As Date, Dend As Date
        
        arr = Sheet1.Range("A1").CurrentRegion.Value 'get the dates in memory
        Dstart = Sheet1.Range("c1").Value
        Dend = Sheet1.Range("c2").Value
        ReDim arr2(1 To UBound(arr), 1 To 1) 'setup temp array
        i = 1 'don't like to start at 0
        For j = 1 To UBound(arr) 'traverse source dates
            If arr(j, 1) >= Dstart And arr(j, 1) <= Dend Then 'check if date is in range
                arr2(i, 1) = arr(j, 1) 'store matching dates in temp array
                i = i + 1
            End If
        Next j
        
        With Sheet2
            .Range(.Cells(1, 1), .Cells(UBound(arr2), 1)).Value = arr2 'paste to sheet
        End With
    End Sub