Merge two ranges together and wrap them into each other

HJA14

Board Regular
Joined
Apr 12, 2016
Messages
60
Hi all,

I want to merge two ranges together. The first range is equal to

Code:
Sheets("1").Range("B1:AA100")

the second range is equal to

Code:
Sheets("2").Range("B1:AA100")

I am familiar with the UNION-function, but I want to merge them differently.
Basically, I want to merge them column for column. Visually it looks as follows:

https://ibb.co/F4d4sdw


I tried using the following for each range

Code:
Sub inser_columns()
For c = 2 to lastColumn Step 2
   Columns(c).Insert Shift:=xlToRight
Next
End Sub
And paste the two new ranges together by skipping blanks. However, this is very slow especially when I increase the sizes of the two ranges.
Help is much appreciated

F4d4sdw
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Try this

Code:
Sub MergeRanges()
    Dim sh1 As Range, sh2 As Range, cel As Range, c As Long
    Set sh1 = Sheets("1").Range("B1:AA100")
    Set sh2 = Sheets("2").Range("B1:AA100")
    Set cel = Sheets("Merge").Cells(1, 2)

    For c = 0 To sh1.Columns.Count - 1
        sh1.Resize(, 1).Offset(, c).Copy cel.Offset(, c * 2)
        sh2.Resize(, 1).Offset(, c).Copy cel.Offset(, c * 2 + 1)
    Next c
End Sub



Excel 2016 (Windows) 32 bit
B
C
D
E
F
G
1
B1C1D1E1F1G1
2
B1C1D1E1F1G1
3
B1C1D1E1F1G1
4
B1C1D1E1F1G1
5
B1C1D1E1F1G1
6
B1C1D1E1F1G1
7
B1C1D1E1F1G1
8
B1C1D1E1F1G1
9
B1C1D1E1F1G1
10
B1C1D1E1F1G1
Sheet: 1

Excel 2016 (Windows) 32 bit
B
C
D
E
F
G
1
B1C1D1E1F1G1
2
B1C1D1E1F1G1
3
B1C1D1E1F1G1
4
B1C1D1E1F1G1
5
B1C1D1E1F1G1
6
B1C1D1E1F1G1
7
B1C1D1E1F1G1
8
B1C1D1E1F1G1
9
B1C1D1E1F1G1
10
B1C1D1E1F1G1
Sheet: 2

RESULT

Excel 2016 (Windows) 32 bit
B
C
D
E
F
G
H
I
J
K
L
M
1
B1B1C1C1D1D1E1E1F1F1G1G1
2
B1B1C1C1D1D1E1E1F1F1G1G1
3
B1B1C1C1D1D1E1E1F1F1G1G1
4
B1B1C1C1D1D1E1E1F1F1G1G1
5
B1B1C1C1D1D1E1E1F1F1G1G1
6
B1B1C1C1D1D1E1E1F1F1G1G1
7
B1B1C1C1D1D1E1E1F1F1G1G1
8
B1B1C1C1D1D1E1E1F1F1G1G1
9
B1B1C1C1D1D1E1E1F1F1G1G1
10
B1B1C1C1D1D1E1E1F1F1G1G1
Sheet: Merge
 
Upvote 0
Since I had been playing around with this too, I will post my offering. This is a bit different as it does not require any looping. My testing showed it to be about 3 times as fast as the post 2 code though I do note that the range size is not very big so that speed difference is basically irrelevant. Anyway, if you feel like it you can give this a whirl as well.
My code assumes ..
- Sheets 'Merge' exists but does not contain data
- In Sheets '1' and '2', the column immediately to the right of the range in question (range AB1:AB100 with the current specs) does not contain data. A modification could be made if that is not the case.

Rich (BB code):
Sub Merge_Ranges()
  Dim a As Variant, vCols As Variant, vRws As Variant
  Dim cel As Range
  
  Const sRngAddress  As String = "B1:AA100" '<- Edit this if you want to adapt the code to a different range

  With Range(sRngAddress)
    vCols = Split(Join(Application.Transpose(Evaluate("row(" & .Column & ":" & .Columns.Count + .Column - 1 & ")")), "," & .Columns.Count + .Column & ","), ",")
    vRws = Evaluate("row(" & .Row & ":" & .Rows.Count + .Row - 1 & ")")
  End With
  Set cel = Sheets("Merge").Range(sRngAddress).Cells(1)
  a = Application.Index(Sheets("1").Cells, vRws, vCols)
  cel.Resize(UBound(a), UBound(a, 2)).Value = a
  a = Application.Index(Sheets("2").Cells, vRws, vCols)
  With cel.Offset(UBound(a)).Resize(UBound(a), UBound(a, 2))
    .Value = a
    .Copy
    cel.Offset(, 1).PasteSpecial xlPasteValues, SkipBlanks:=True
    .ClearContents
 End With
End Sub
 
Upvote 0
Hi,
You might try this as well, if you like
Code:
Sub Merge()
    Dim sh1 As Range, sh2 As Range, cel As Range, c As Long
    a = Application.Transpose(Sheet1.Range("B1").CurrentRegion)
    b = Application.Transpose(Sheet2.Range("B1").CurrentRegion)
    Set dest = Sheet3
    k = 1
    cl = UBound(a, 2)
    dest.Activate
    For i = 1 To UBound(a, 1) * 2 - 1 Step 2
        Range(Cells(1, i), Cells(cl, i)).Offset(, 1) = Application.Transpose(Application.Index(a, k, 0))
        Range(Cells(1, i + 1), Cells(cl, i + 1)).Offset(, 1) = Application.Transpose(Application.Index(b, k, 0))
        k = k + 1
    Next i
End Sub

Flexible, Fast, ....
 
Upvote 0

Forum statistics

Threads
1,214,615
Messages
6,120,538
Members
448,970
Latest member
kennimack

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