copy matrix to worksheet

  • Thread starter MÃ¥ns TÃ¥nneryd
  • Start date
M

MÃ¥ns TÃ¥nneryd

Hi!

I have a two-dimensionnal matrix and i would like copy the contents of this
matrix directly to the cells in a worksheet without iterating over each cell
one by one. Is this possible? Anyone out there that knows?

Thanks!

/MÃ¥ns TÃ¥nneryd
 
C

Chip Pearson

If by "matrix" you mean a range of cells on a worksheet, you can use
code like the following:

Dim Matrix As Range
Dim Dest As Range
Set Matrix = Range("B4:C7") '<<< CHANGE AS REQUIRED
Set Dest = Range("F10") '<<< CHANGE AS REQUIRED
Matrix.Copy Destination:=Dest

This copies the range B4:C7 to a range starting at F10.


If by "matrix" you mean a 2 dimensional array, you can use code like
the following:

Dim Matrix(1 To 2, 1 To 2) As Long
Dim R As Long
Dim C As Long
' load up test values
Matrix(1, 1) = 11
Matrix(1, 2) = 22
Matrix(2, 1) = 33
Matrix(2, 2) = 44
' get number of rows and columns
R = UBound(Matrix, 1) - LBound(Matrix, 1) + 1
C = UBound(Matrix, 2) - LBound(Matrix, 2) + 1
' resize the destination range
Range("F10").Resize(R, C).Value = Matrix


This writes the contents of the Matrix array to cell F10.



Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top