Excel VBA function to print an array to the workbook

后端 未结 6 1747
心在旅途
心在旅途 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 03:57

    A more elegant way is to assign the whole array at once:

    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
    

    But watch out: it only works up to a size of about 8,000 cells. Then Excel throws a strange error. The maximum size isn't fixed and differs very much from Excel installation to Excel installation.

提交回复
热议问题