测试数组给单元格赋值

Sub 测试数组的常见操作()
    Dim arr()
    Dim totalR As Integer
    totalR = Range("A65536").End(xlUp).Row '值为12
    arr = Range(Cells(1, 1), Cells(totalR, 2)).Value '共12行
    Debug.Print LBound(arr) '值为1
    Debug.Print UBound(arr) '值为12
    '通过水平转置,可以将数据原为两列,改为两行.也证明了arr数组默认为列.
     '将整个数组经水平转置后赋值给相同范围的单元格区域.
    Range(Cells(1, 3), Cells(2, UBound(arr) + 3 - 1)).Value = Application.WorksheetFunction.Transpose(arr())
    '将数组经水平转置后取第1行的数据赋值给相同范围的单元格区域
    Range(Cells(3, 3), Cells(3, UBound(arr) + 3 - 1)).Value = Application.WorksheetFunction.Index(Application.WorksheetFunction.Transpose(arr), 1, 0)
    '将数组的第2列数据赋值给相同范围的单元格区域
    Range(Cells(4, 3), Cells(UBound(arr) + 4 - 1, 3)).Value = Application.WorksheetFunction.Index(arr, 0, 2)
    Dim i As Integer
    For i = 1 To UBound(arr)
        Debug.Print arr(i, 2) '证明了由数据区域获得的数组下标默认为从1开始.
    Next i
    '验证如何输出,即使只是一列的数组,也要在arr(i,1)中指明列数(即为1)
End Sub

菊子曰 今天你菊子曰了么?
posted @ 2010-05-11 12:04  surfacetension  阅读(723)  评论(0编辑  收藏  举报