Merge Same Column Name

christian2016

Board Regular
Joined
Oct 6, 2016
Messages
123
Hi Guys,

Need help on how to merge same column names with data.

Example
City | State | Address | Address | Address

Now I would like to loop through the column headers and if finds the same column header name in this example would be address it will combined all the text information in all columns with spaces in-between.
All information will be stored in one column called address and the other 2 address columns will be deleted.

Unsure on how to approach this to make something dynamic.

Any help is greatly appreciated.

Thanks
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Like this?

VBA Code:
Sub test()
Dim Arr() As Variant
Lr = Range("A2").CurrentRegion.Rows.Count
Lc = Range("A2").CurrentRegion.Columns.Count
ReDim Arr(Lr - 1)
firstAdd = Range(Cells(1, 1), Cells(1, Lc)).Find("Address").Offset(0, 1).Address
LastAdd = Range(Cells(1, 1), Cells(1, Lc)).Find("Address", , , , xlByRows, xlPrevious).Address
For i = 2 To Lr
  For j = 1 To Lc
    If Cells(1, j).Value = "Address" Then
       combi = combi & Cells(i, j) & " "
    End If
  Next
Arr(i - 2) = Trim(combi)
combi = ""
Next
    With Range(firstAdd)
    For t = 0 To UBound(Arr)
    .Offset(t + 1, -1) = Arr(t)
    Next
    Range(firstAdd, LastAdd).EntireColumn.Delete
    Cells.Columns.AutoFit
    Range("A1").Select
    End With
End Sub
 

Attachments

  • Before.JPG
    Before.JPG
    32.3 KB · Views: 21
  • after.JPG
    after.JPG
    24.9 KB · Views: 21
Upvote 0
I would like to loop through the column headers and if finds the same column header name
This will look for any repeat header names, not just 'Address'. Test with a copy of your worksheet.
I have assumed that column A can be used to locate the bottom of the data.

VBA Code:
Sub Combine_Columns()
  Dim c As Long, j As Long, lr As Long, cc As Long
  Dim Hdr As String
  
  lr = Range("A" & Rows.Count).End(xlUp).Row
  Application.ScreenUpdating = False
  For c = Cells(1, Columns.Count).End(xlToLeft).Column To 2 Step -1
    If Not IsEmpty(Cells(1, c).Value) Then
      Hdr = Cells(1, c).Value
      cc = c
      For j = c - 1 To 1 Step -1
        If Cells(1, j).Value = Hdr Then
          With Cells(2, j).Resize(lr - 1)
            .Value = Evaluate("trim(" & .Address & "&"" ""&" & .Offset(, cc - j).Address & ")")
          End With
          Columns(cc).Delete
          cc = j
        End If
      Next j
    End If
  Next c
  ActiveSheet.UsedRange.Columns.AutoFit
  Application.ScreenUpdating = True
End Sub

Before

christian2016.xlsm
ABCDEF
1CityStateAddressStateAddressAddress
2City 12State 2Address 11State 6Address 3
3City 9State 17Address 9
4City 6State 1Address 99
5City 15State 11Address 19Address 8
6City 16State 6Address 5State 6Address 8Address 16
7City 7State 19Address 19State 7Address 15
8City 19State 3Address 2State 1Address 12Address 1
9City 1State 5Address 7
Sheet1



After

christian2016.xlsm
ABC
1CityStateAddress
2City 12State 2 State 6Address 11 Address 3
3City 9State 17Address 9
4City 6State 1Address 99
5City 15State 11Address 19 Address 8
6City 16State 6 State 6Address 5 Address 8 Address 16
7City 7State 19 State 7Address 19 Address 15
8City 19State 3 State 1Address 2 Address 12 Address 1
9City 1State 5Address 7
Sheet1
 
Upvote 0
This will look for any repeat header names, not just 'Address'. Test with a copy of your worksheet.
I have assumed that column A can be used to locate the bottom of the data.

VBA Code:
Sub Combine_Columns()
  Dim c As Long, j As Long, lr As Long, cc As Long
  Dim Hdr As String
 
  lr = Range("A" & Rows.Count).End(xlUp).Row
  Application.ScreenUpdating = False
  For c = Cells(1, Columns.Count).End(xlToLeft).Column To 2 Step -1
    If Not IsEmpty(Cells(1, c).Value) Then
      Hdr = Cells(1, c).Value
      cc = c
      For j = c - 1 To 1 Step -1
        If Cells(1, j).Value = Hdr Then
          With Cells(2, j).Resize(lr - 1)
            .Value = Evaluate("trim(" & .Address & "&"" ""&" & .Offset(, cc - j).Address & ")")
          End With
          Columns(cc).Delete
          cc = j
        End If
      Next j
    End If
  Next c
  ActiveSheet.UsedRange.Columns.AutoFit
  Application.ScreenUpdating = True
End Sub

Before

christian2016.xlsm
ABCDEF
1CityStateAddressStateAddressAddress
2City 12State 2Address 11State 6Address 3
3City 9State 17Address 9
4City 6State 1Address 99
5City 15State 11Address 19Address 8
6City 16State 6Address 5State 6Address 8Address 16
7City 7State 19Address 19State 7Address 15
8City 19State 3Address 2State 1Address 12Address 1
9City 1State 5Address 7
Sheet1



After

christian2016.xlsm
ABC
1CityStateAddress
2City 12State 2 State 6Address 11 Address 3
3City 9State 17Address 9
4City 6State 1Address 99
5City 15State 11Address 19 Address 8
6City 16State 6 State 6Address 5 Address 8 Address 16
7City 7State 19 State 7Address 19 Address 15
8City 19State 3 State 1Address 2 Address 12 Address 1
9City 1State 5Address 7
Sheet1
This is exactly what im after. I will give it a go and see how it works. Thanks again ?
 
Upvote 0
it works but for some odd reason it only works for the first row.
First thought is: Did you check my assumption?
I have assumed that column A can be used to locate the bottom of the data.

Try this instead

VBA Code:
Sub Combine_Columns_v2()
  Dim c As Long, j As Long, lr As Long, cc As Long
  Dim Hdr As String
  
  lr = Cells.Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
  Application.ScreenUpdating = False
  For c = Cells(1, Columns.Count).End(xlToLeft).Column To 2 Step -1
    If Not IsEmpty(Cells(1, c).Value) Then
      Hdr = Cells(1, c).Value
      cc = c
      For j = c - 1 To 1 Step -1
        If Cells(1, j).Value = Hdr Then
          With Cells(2, j).Resize(lr - 1)
            .Value = Evaluate("trim(" & .Address & "&"" ""&" & .Offset(, cc - j).Address & ")")
          End With
          Columns(cc).Delete
          cc = j
        End If
      Next j
    End If
  Next c
  ActiveSheet.UsedRange.Columns.AutoFit
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
First thought is: Did you check my assumption?


Try this instead

VBA Code:
Sub Combine_Columns_v2()
  Dim c As Long, j As Long, lr As Long, cc As Long
  Dim Hdr As String
 
  lr = Cells.Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
  Application.ScreenUpdating = False
  For c = Cells(1, Columns.Count).End(xlToLeft).Column To 2 Step -1
    If Not IsEmpty(Cells(1, c).Value) Then
      Hdr = Cells(1, c).Value
      cc = c
      For j = c - 1 To 1 Step -1
        If Cells(1, j).Value = Hdr Then
          With Cells(2, j).Resize(lr - 1)
            .Value = Evaluate("trim(" & .Address & "&"" ""&" & .Offset(, cc - j).Address & ")")
          End With
          Columns(cc).Delete
          cc = j
        End If
      Next j
    End If
  Next c
  ActiveSheet.UsedRange.Columns.AutoFit
  Application.ScreenUpdating = True
End Sub
Yes all my headers are in column A. That works well. Thank you so much :)
 
Upvote 0
Glad that resolved the issue. Thanks for letting us know. :)
 
Upvote 0

Forum statistics

Threads
1,224,755
Messages
6,180,771
Members
452,996
Latest member
nelsonsix66

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