21-VBA数组的读取

一、在内存中读取数据

如果是一维数组存入单元格中要竖直放置需要转置。

Sub t1()
    Dim arr, arr1(), m, x, k
    arr = Range("a1:a10")
    m = Application.WorksheetFunction.CountIf(Range("a1:a10"), ">10")
    ReDim arr1(1 To m)
    For x = 1 To 10
        If arr(x, 1) > 10 Then
            k = k + 1
            arr1(k) = arr(x, 1)
        End If
    Next x
    Range("d2").Resize(5) = Application.Transpose(arr1)
End Sub

二、读取存入单元格中

Sub t2()
    Dim arr(), arr1(1 To 5, 1 To 1), x
    arr() = Range("b2:c6")
        For x = 1 To 5
            arr1(x, 1) = arr(x, 1) * arr(x, 2)
        Next x
    'Range("d2:d6") = arr1
    Range("d2").Resize(5) = arr1
End Sub