Excel VBA - copy area 8 columns by X rows, paste as 4 columns by 2X rows?

Tomkat53

Board Regular
Joined
Oct 22, 2010
Messages
61
So basically I have data arragned like...

1,2,3,4,5,6,7,8
1,2,3,4,5,6,7,8
1,2,3,4,5,6,7,8
........

**NOTE** The source range is dynamic, not static, so although it is always 8 columns, it can be any number of rows.
I'm using Selection.End(xlDown), etc to copy the source range to account for this.

Then I want to copy/paste/re-arrange via macro so that the paste destination looks like...

1,2,3,4
5,6,7,8
1,2,3,4
5,6,7,8
1,2,3,4
5,6,7,8
....
....

I know I should be able to do this, and I've searched for the answer, but I think I'm not describing my issue with the correct terms.
Can anyone please help me?
 
Last edited:

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: Copy 8 columns of data, paste "stacked" into 4 colums?
There is no need to repeat the link(s) provided above but if you have posted the question at other places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0
Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: Copy 8 columns of data, paste "stacked" into 4 colums?
There is no need to repeat the link(s) provided above but if you have posted the question at other places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.


I apologize, but they looked like completely different forums, to me.
I even have separate logins.

I'm confused. What's the deal?
 
Upvote 0
No idea where your data starts (A1?) or where the destination is, but please adjust the following code to suit your actual case. This demo assumes contiguous data from A1 & the destination is J1.

VBA Code:
Option Explicit
Sub Stack4()
    Dim a, b, i As Long, j As Long, k As Long
    a = Range("A1").CurrentRegion
    ReDim b(1 To UBound(a, 1) * 2, 1 To 4)
    j = 1
    
    For i = 1 To UBound(b, 1)
        For k = 1 To 4
            b(i, k) = a(j, k)
        Next k
        i = i + 1
        For k = 1 To 4
            b(i, k) = a(j, k + 4)
        Next k
        j = j + 1
    Next i
    Range("J1").Resize(UBound(b, 1), 4) = b
End Sub

Before & after:
Book1
ABCDEFGHIJKLM
1123456781234
2123456785678
3123456781234
4123456785678
5123456781234
6123456785678
7123456781234
8123456785678
9123456781234
10123456785678
111234
125678
131234
145678
151234
165678
171234
185678
191234
205678
21
Sheet1
 
Upvote 0
Solution
No idea where your data starts (A1?) or where the destination is, but please adjust the following code to suit your actual case. This demo assumes contiguous data from A1 & the destination is J1.

VBA Code:
Option Explicit
Sub Stack4()
    Dim a, b, i As Long, j As Long, k As Long
    a = Range("A1").CurrentRegion
    ReDim b(1 To UBound(a, 1) * 2, 1 To 4)
    j = 1
  
    For i = 1 To UBound(b, 1)
        For k = 1 To 4
            b(i, k) = a(j, k)
        Next k
        i = i + 1
        For k = 1 To 4
            b(i, k) = a(j, k + 4)
        Next k
        j = j + 1
    Next i
    Range("J1").Resize(UBound(b, 1), 4) = b
End Sub

Before & after:
Book1
ABCDEFGHIJKLM
1123456781234
2123456785678
3123456781234
4123456785678
5123456781234
6123456785678
7123456781234
8123456785678
9123456781234
10123456785678
111234
125678
131234
145678
151234
165678
171234
185678
191234
205678
21
Sheet1

I forgot to mention that the source and destination are on different worksheets.
I think I can modify this code to account for that... thanks!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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