Jwok
Jwok

Reputation: 742

Paste only specific indexes of 2-d Array in VBA

I have a 2-day array like the following:

myArray(1 to 100, 1 to 3)

My normal way to paste an array is like this:

With ActiveWorkbook.Worksheets("Semesters")

If IsEmpty(.Range("A2")) Then
    .Activate
    .Range("A2", "C2").Resize(UBound(myArray)).Value = myArray
Else
    .Activate
    .Range("A1").End(xlDown).Offset(1, 0).Activate
    Range(ActiveCell, ActiveCell.Offset(0, 2)).Resize(UBound(myArray)).Value = myArray
End If

End With

But this pastes the entire array. I want to paste all of the first dimension, but only values at index 2 and 3 of the second dimension. Can this be done without creating another array that filters out index 1 of dimension 2, and if so, how?

Upvotes: 3

Views: 241

Answers (1)

user4039065
user4039065

Reputation:

You cannot do this in one step but you can do it in two.

.Range("A2").Resize(UBound(myArray, 1)) = application.index(myArray, 0, 2)
.Range("C2").Resize(UBound(myArray, 1)) = application.index(myArray, 0, 3)

Upvotes: 3

Related Questions