Copying Some rows (one column) from a 2-D array to a particular column of another array

Saugata

New Member
Joined
Sep 28, 2022
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hi,

I am stuck. I am trying to copy some selected rows from one column of a 2-D array to a particular column of another 2-D Array. I have tried all my knowledge, but to no avail.

VBA Code:
Sub TestIndxPst()
    Dim varArray As Variant
    
    ReDim varArray(1 To 4, 1 To 3)
    Dim vaOut As Variant
    ReDim vaOut(1 To 4, 1 To 3)
    RowNumArray = Evaluate("transpose(Row(1" & ":" & 5 & "))")
    varArray = ThisWorkbook.Worksheets("Sheet1").Range("G2:I7")
    Application.Index([G9:I12], , 2) = Application.Transpose(Application.Index(varArray, Array(2, 3, 5, 6), 2))
    vaOut = Application.Transpose(Application.Index(varArray, Array(2, 3, 5, 6), 2))
    Application.Index(vaOut, , 2) = Application.Transpose(Application.Index(varArray, Array(2, 3, 5, 6), 2))
    For i = LBound(vaOut) To UBound(vaOut)
        Debug.Print vaOut(i, 1)
    Next i
End Sub

You can see that I can do that while writing to an excel range in Application.Index([G9:I12], , 2) = Application.Transpose(Application.Index(varArray, Array(2, 3, 5, 6), 2))
I can also do that when I output it to the first column using vaOut = Application.Transpose(Application.Index(varArray, Array(2, 3, 5, 6), 2))
But as soon as I am trying to put the result in column 2 of out array here, Application.Index(vaOut, , 2) = Application.Transpose(Application.Index(varArray, Array(2, 3, 5, 6), 2)). I get application-defined or object defined error

Any help is highly appreciated.

FYI, my sample input, if anyone is interested.
1664348741389.png
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Hello
Try this

Sub tst_fill_ar()
Dim src_arr(5, 3), dst_arr(5, 3) As Variant
Dim irow, icol As Integer

'Here we store data from the worsheet to the the source array

For icol = 0 To UBound(src_arr, 2)
For irow = 0 To UBound(src_arr, 1)
src_arr(irow, icol) = ThisWorkbook.Worksheets("feuil3").Cells(irow + 1, icol + 1)
Debug.Print src_arr(irow, icol)
Next irow
Next icol

'Here we store data from the source array to the destination array

For icol = 0 To UBound(dst_arr, 2)
For irow = 0 To UBound(dst_arr, 1)
dst_arr(irow, icol) = src_arr(irow, icol)
Debug.Print dst_arr(irow, icol)
Next irow
Next icol


End Sub
 
Upvote 0
What you must keep in mind is that the transpose method makes a row from a source a column in the destination. In your case it is not what you want to do.
Secondly the way you declare the array is not the right way. Check the example i showed you.

Hope you will find this helpful
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,691
Members
448,978
Latest member
rrauni

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top