Thank you very much DanteAmor!Change "A:C" for the columns you want to join. The union will be in the next column.
VBA Code:Sub Merge_Columns() Dim j As Range, r As Range Dim lr1 As Long, lr2 As Long lr2 = 1 Set r = Range("A:C") 'columns to merge For Each j In r.Columns lr1 = Cells(Rows.Count, j.Column).End(3).Row r.Cells(lr2, r.Columns.Count + 1).Resize(lr1).Value = j.Range(Cells(1, 1), Cells(lr1, 1)).Value lr2 = r.Cells(Rows.Count, r.Columns.Count + 1).End(3).Row + 1 Next End Sub
Try this:join for example column A, column J and column Z to AA Column
Sub Merge_Columns()
Dim j As Range, r As Range
Dim lr1 As Long, lr2 As Long
Dim col As String
lr2 = 1
Set r = Range("A:A, J:J, Z:Z") 'columns to join
col = "AA" 'detination column
For Each j In r.Columns
lr1 = Cells(Rows.Count, j.Column).End(3).Row
r.Cells(lr2, col).Resize(lr1).Value = j.Range(Cells(1, 1), Cells(lr1, 1)).Value
lr2 = r.Cells(Rows.Count, col).End(3).Row + 1
Next
End Sub
It is strange. If i keep the code as is, it merges A, J & Z columns.Try this:
VBA Code:Sub Merge_Columns() Dim j As Range, r As Range Dim lr1 As Long, lr2 As Long Dim col As String lr2 = 1 Set r = Range("A:A, J:J, Z:Z") 'columns to join col = "AA" 'detination column For Each j In r.Columns lr1 = Cells(Rows.Count, j.Column).End(3).Row r.Cells(lr2, col).Resize(lr1).Value = j.Range(Cells(1, 1), Cells(lr1, 1)).Value lr2 = r.Cells(Rows.Count, col).End(3).Row + 1 Next End Sub
Sub Merge_Columns()
Dim j As Range, r As Range
Dim lr1 As Long, lr2 As Long
Dim col As String
lr2 = 1
Set r = Range("Z:Z, AE:AE, AJ:AJ") 'columns to join
col = "AA" 'detination column
For Each j In r.Columns
lr1 = Cells(Rows.Count, j.Column).End(3).Row
Cells(lr2, col).Resize(lr1).Value = j.Range(Cells(1, 1), Cells(lr1, 1)).Value
lr2 = Cells(Rows.Count, col).End(3).Row + 1
Next
End Sub
This works perfectly!I forgot to remove the range r.
Try this:
VBA Code:Sub Merge_Columns() Dim j As Range, r As Range Dim lr1 As Long, lr2 As Long Dim col As String lr2 = 1 Set r = Range("Z:Z, AE:AE, AJ:AJ") 'columns to join col = "AA" 'detination column For Each j In r.Columns lr1 = Cells(Rows.Count, j.Column).End(3).Row Cells(lr2, col).Resize(lr1).Value = j.Range(Cells(1, 1), Cells(lr1, 1)).Value lr2 = Cells(Rows.Count, col).End(3).Row + 1 Next End Sub
but thing returns "Run-time error 1004" when running the script.Set r = Range("Z5:Z, AE5:AE, AJ5:AJ")
My data on columns begin from 5th row
Sub Merge_Columns()
Dim j As Range, r As Range
Dim lr1 As Long, lr2 As Long, ini As Long
Dim col As String
Set r = Range("Z:Z, AE:AE, AJ:AJ") 'columns to join
ini = 5 'row start origen
col = "AA" 'detination column
lr2 = 5 'row start destination
For Each j In r.Columns
lr1 = WorksheetFunction.Max(Cells(Rows.Count, j.Column).End(3).Row, ini)
Cells(lr2, col).Resize(lr1 - (ini - 1)).Value = j.Range(Cells(ini, 1), Cells(lr1, 1)).Value
lr2 = WorksheetFunction.Max(Cells(Rows.Count, col).End(3).Row + 1, lr2)
Next
End Sub
You are truly amazing DanteAmor! Thank you so much! I really appreciate your help!I made some changes to put the starting row of origin and the starting row of destination.
VBA Code:Sub Merge_Columns() Dim j As Range, r As Range Dim lr1 As Long, lr2 As Long, ini As Long Dim col As String Set r = Range("Z:Z, AE:AE, AJ:AJ") 'columns to join ini = 5 'row start origen col = "AA" 'detination column lr2 = 5 'row start destination For Each j In r.Columns lr1 = WorksheetFunction.Max(Cells(Rows.Count, j.Column).End(3).Row, ini) Cells(lr2, col).Resize(lr1 - (ini - 1)).Value = j.Range(Cells(ini, 1), Cells(lr1, 1)).Value lr2 = WorksheetFunction.Max(Cells(Rows.Count, col).End(3).Row + 1, lr2) Next End Sub