Carlsberg789
Carlsberg789

Reputation: 155

VBA array exporting to sheet

I have an array with values in it, and i want to paste several columns to two parts of the same sheet. First matrix would be pasted in column B and second in column H.

Is it possible to export several columns (not one and not all) with a single procedure? So far i have the options below, i can export one or all of the columns but not "a few".

Worksheets("Sheet1").Range("B" & m).Resize(s, 14).Value = Matrix3 'Exports entire matrix
Worksheets("E02 Sheet2").Range("B" & o).Resize(t, 9).Value = Application.Index(Matrix, 0, 1) 'Exports a single column of a matrix

Upvotes: 1

Views: 693

Answers (1)

Storax
Storax

Reputation: 12167

Something like that is possible: Let's assume we have a sheet like in the picture below enter image description here

The following code will put the first and third column into the variable vDat and write it back.

Sub TestIt()
    Dim matrix As Variant
    matrix = Sheet1.Range("A1:C3")

    Dim vDat As Variant
    vDat = Application.Index(matrix, Array(1, 2, 3), Application.Transpose(Array(1, 3)))

    Sheet1.Range("E1").Resize(3, 2).Value = Application.Transpose(vDat)

End Sub

You "only" need to modify the code to your needs

Upvotes: 3

Related Questions