2d array pasting into single column

zpierucci

New Member
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?
 

Eric W

MrExcel MVP
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.
 

mohadin

Active Member
What about
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
 
Last edited:

Eric W

MrExcel MVP
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! :)
 
Last edited:

zpierucci

New Member
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?
 

Eric W

MrExcel MVP
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.
 

zpierucci

New Member
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?
 

Some videos you may like

This Week's Hot Topics

  • Importing multiple excel files into one spreadsheet
    Hi, I'm trying to import multiple excel files (with the same format into a single spreadsheet) so that each day's file is listed underneath the...
  • find many based on a certain criteria
    good evening, I hope someone can help me? I have a workbook sheet 2 contains lots of data.... I would like to be able to find anything on sheet...
  • How to copy multiple rows using If
    Hi all, I'm very new to VBA and have written this simple code to copy certain cells if a certain cell within that row contains any data. I need...
  • VBA If statement
    Dear All, I have two dates, where I'd like a message box to pop, if the dates are between this criteria. [CODE] sDate1 = #10/1/2019#...
  • Text Format
    I have a sheet for user to keyin the data. The format of the data can be 451 / 1903, 0012 / 9908 or 00287 / 0099. The number after the "/" is...
  • Macro to copy values across rows and transposing them and add the user id
    [FONT=Times New Roman][SIZE=3][COLOR=#000000][/COLOR][/SIZE][/FONT][FONT=Calibri][SIZE=3][COLOR=#000000]Hi,[/COLOR][/SIZE][/FONT] [FONT=Times New...
Top