# Thread: 2d array pasting into single column Thanks: 0 Likes: 0

1. ## 2d array pasting into single column

I have 6 columns (B:G) with 16 rows (2:17) of data in each column. I am trying to get all of the info of the array into one single column starting at cell K4. I have been able to write a loop to go down the rows but I can't seem to get the loops right. Any suggestions?  Reply With Quote

2. ## Re: 2d array pasting into single column

When you say "write a loop" I assume you mean VBA. If so, here is a basic loop to do that:

Code:
```    r2 = 4
For c = 2 To 7
For r = 2 To 17
Cells(r2, "K") = Cells(r, c)
r2 = r2 + 1
Next r
Next c```
This is very basic so you can see the main idea. There are somewhat more complicated ways that are much faster. You probably wouldn't notice a difference on such a small sample though. It's also worth noting that you can do this with a formula as well.  Reply With Quote

3. ## Re: 2d array pasting into single column

Thanks Eric. That worked perfect.  Reply With Quote

4. ## Re: 2d array pasting into single column

Code:
```Sub test()
c = 4
ar = Range("b2:g17")
For i = 1 To 6
Range("k" & c & ":k" & UBound(ar, 1) + c - 1) = Application.Index(ar, 0, i)
c = c + UBound(ar, 1)
Next
End Sub```
OR
Code:
```Sub test()
c = 4
ar = Application.Transpose(Range("b2:g17"))
For i = 1 To 16
Range("k" & c & ":k" & UBound(ar, 1) + c - 1) = Application.Index(ar, 0, i)
c = c + UBound(ar, 1)
Next
End Sub```  Reply With Quote

5. ## Re: 2d array pasting into single column

mohadin showed a few other options, which would be more efficient on large samples. Here's one more:

Code:
```    For c = 2 To 7
Range("B2:B17").Offset(, c - 2).Copy Range("K4").Offset((c - 2) * 16).Resize(16)
Next c```
This used Copy instead of just moving the values. Depends if you want to keep the formatting too or not. The Offset and Resize methods can be really useful. Excel always has about 10 ways to do anything, it just depends on the situation.

Glad we could help!   Reply With Quote

6. ## Re: 2d array pasting into single column

Thanks guys. This is awesome. Not so worried about the formatting for this but good info. One more question, if I had to do the same thing but the columns or "ranges" were not contiguous? So I still had 6 rows of data but they were separated by other data?  Reply With Quote

7. ## Re: 2d array pasting into single column

Again, lots of ways to do this. Here's a slight modification of my original macro:

Code:
```Sub test2()
Dim cols As Variant, r As Long, c As Long, r2 As Long

cols = Array(2, 3, 4, 5, 6, 7)
r2 = 4
For c = LBound(cols) To UBound(cols)
For r = 2 To 17
Cells(r2, "K") = Cells(r, cols(c))
r2 = r2 + 1
Next r
Next c
End Sub```
Just put the columns you want in the Cols array. If the columns are consistent distances apart, you can just add that amount to c every time.  Reply With Quote

8. ## Re: 2d array pasting into single column

Worked perfect again! Thanks a lot. Separate issue, I haven't done much VBA coding for years. I really enjoy it and am trying to get proficient in it again. Is there any online resources that you would recommend for practice problem sets or good exercises?  Reply With Quote

9. ## Re: 2d array pasting into single column

One of the members here, @hiker95 , has compiled an enormous list of online resources for learning anything Excel-related, including VBA. Check out post 3 on this thread:

https://www.mrexcel.com/forum/genera...excel-vba.html

Find something on the list that looks interesting and start there. Glad we could help!  Reply With Quote

## User Tag List

array, column, rows, single, write 