Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paste only specific indexes of 2-d Array in VBA

Tags:

arrays

excel

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?

like image 868
Jwok Avatar asked Dec 09 '25 12:12

Jwok


1 Answers

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)

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!