Excel VBA函数将数组打印到工作簿

时间:2011-05-19 19:07:39

标签: excel vba excel-vba

我编写了一个采用二维数组的宏,并将其“打印”到excel工作簿中的等效单元格。

有更优雅的方法吗?

Sub PrintArray(Data, SheetName, StartRow, StartCol)

    Dim Row As Integer
    Dim Col As Integer

    Row = StartRow

    For i = LBound(Data, 1) To UBound(Data, 1)
        Col = StartCol
        For j = LBound(Data, 2) To UBound(Data, 2)
            Sheets(SheetName).Cells(Row, Col).Value = Data(i, j)
            Col = Col + 1
        Next j
            Row = Row + 1
    Next i

End Sub


Sub Test()

    Dim MyArray(1 To 3, 1 To 3)
    MyArray(1, 1) = 24
    MyArray(1, 2) = 21
    MyArray(1, 3) = 253674
    MyArray(2, 1) = "3/11/1999"
    MyArray(2, 2) = 6.777777777
    MyArray(2, 3) = "Test"
    MyArray(3, 1) = 1345
    MyArray(3, 2) = 42456
    MyArray(3, 3) = 60

    PrintArray MyArray, "Sheet1", 1, 1

End Sub

6 个答案:

答案 0 :(得分:16)

与其他答案相同的主题,保持简单

Sub PrintArray(Data As Variant, Cl As Range)
    Cl.Resize(UBound(Data, 1), UBound(Data, 2)) = Data
End Sub


Sub Test()
    Dim MyArray() As Variant

    ReDim MyArray(1 To 3, 1 To 3) ' make it flexible

    ' Fill array
    '  ...

    PrintArray MyArray, ActiveWorkbook.Worksheets("Sheet1").[A1]
End Sub

答案 1 :(得分:3)

创建变量数组(最简单的方法是读取变量变量的等效范围)。

然后填充数组,并将数组直接指定给范围。

Dim myArray As Variant

myArray = Range("blahblah")

Range("bingbing") = myArray

变体数组最终将成为二维矩阵。

答案 2 :(得分:1)

我的测试版

Sub PrintArray(RowPrint, ColPrint, ArrayName, WorkSheetName)

Sheets(WorkSheetName).Range(Cells(RowPrint, ColPrint), _
Cells(RowPrint + UBound(ArrayName, 2) - 1, _
ColPrint + UBound(ArrayName, 1) - 1)) = _
WorksheetFunction.Transpose(ArrayName)

End Sub

答案 3 :(得分:1)

正如其他人所建议的那样,您可以直接将2维数组写入工作表范围内,但如果您的数组是单维的,那么您有两个选择:

  1. 首先将1D数组转换为2D数组,然后将其打印在工作表上(作为范围)。
  2. 将您的1D数组转换为字符串并将其打印在单个单元格中(作为字符串)。
  3. 以下是描述这两个选项的示例:

    Sub PrintArrayIn1Cell(myArr As Variant, cell As Range)
        cell = Join(myArr, ",")
    End Sub
    Sub PrintArrayAsRange(myArr As Variant, cell As Range)
        cell.Resize(UBound(myArr, 1), UBound(myArr, 2)) = myArr
    End Sub
    Sub TestPrintArrayIntoSheet()  '2dArrayToSheet
        Dim arr As Variant
        arr = Split("a  b  c", "  ")
    
        'Printing in ONE-CELL: To print all array-elements as a single string separated by comma (a,b,c):
        PrintArrayIn1Cell arr, [A1]
    
        'Printing in SEPARATE-CELLS: To print array-elements in separate cells:
        Dim arr2D As Variant
        arr2D = Application.WorksheetFunction.Transpose(arr) 'convert a 1D array into 2D array
        PrintArrayAsRange arr2D, Range("B1:B3")
    End Sub
    

    注意:Transpose将呈现逐列输出,以获得逐行输出再次转置它 - 希望这是有意义的。

    HTH

答案 4 :(得分:0)

更优雅的方法是一次分配整个数组:

Sub PrintArray(Data, SheetName, StartRow, StartCol)

    Dim Rng As Range

    With Sheets(SheetName)
        Set Rng = .Range(.Cells(StartRow, StartCol), _
            .Cells(UBound(Data, 1) - LBound(Data, 1) + StartRow, _
            UBound(Data, 2) - LBound(Data, 2) + StartCol))
    End With
    Rng.Value2 = Data

End Sub

但请注意:它只能达到约8,000个细胞的大小。然后Excel抛出一个奇怪的错误。最大大小不固定,与Excel安装到Excel安装有很大不同。

答案 5 :(得分:0)

您可以定义范围,数组的大小并使用它的value属性:

Sub PrintArray(Data, SheetName As String, intStartRow As Integer, intStartCol As Integer)

    Dim oWorksheet As Worksheet
    Dim rngCopyTo As Range
    Set oWorksheet = ActiveWorkbook.Worksheets(SheetName)

    ' size of array
    Dim intEndRow As Integer
    Dim intEndCol As Integer
    intEndRow = UBound(Data, 1)
    intEndCol = UBound(Data, 2)

    Set rngCopyTo = oWorksheet.Range(oWorksheet.Cells(intStartRow, intStartCol), oWorksheet.Cells(intEndRow, intEndCol))
    rngCopyTo.Value = Data

End Sub