Excel VBA function to print an array to the workbook

后端 未结 6 1749
心在旅途
心在旅途 2020-12-10 03:17

I\'ve written a macro that takes a 2 dimensional array, and \"prints\" it to equivalent cells in an excel workbook.

Is there a more elegant way to do this?



        
6条回答
  •  暖寄归人
    2020-12-10 04:20

    You can define a Range, the size of your array and use it's value property:

    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
    

提交回复
热议问题