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

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Having another issue now. Didn't pick it up before maybe because I was using the same data set.

Example

1643202777044.png


1643202794068.png
What is "the issue"?

Is that bottom image what you are getting or what you want to get?
If it is what you are getting, what do you want to get?
 
Upvote 0
What is "the issue"?

Is that bottom image what you are getting or what you want to get?
If it is what you are getting, what do you want to get?
1643242831774.png


So basically any column header that has the same name I need to combined the column with the row data with a space between the combined data.
 
Upvote 0
If that image is what you want then as far as I can see, the _v2 code does that.

Here is my sample sheet before the v2 code ..

christian2016.xlsm
ABCDE
1PersonMatrixMatrixMatrixColour
2Male124589Blue
3MalePurple
4Female654542Pink
New


.. and after the v2 code

christian2016.xlsm
ABCDE
1PersonMatrixColour
2Male12 45 89Blue
3MalePurple
4Female65 45 42Pink
New
 
Upvote 0
If that image is what you want then as far as I can see, the _v2 code does that.

Here is my sample sheet before the v2 code ..

christian2016.xlsm
ABCDE
1PersonMatrixMatrixMatrixColour
2Male124589Blue
3MalePurple
4Female654542Pink
New


.. and after the v2 code

christian2016.xlsm
ABCDE
1PersonMatrixColour
2Male12 45 89Blue
3MalePurple
4Female65 45 42Pink
New
for some strange reason when i use the V2 code i dont get that result. I get the same values from the first row in all the merged column rows
 
Upvote 0
VBA Code:
Sub Combine_Columns()

Application.ScreenUpdating = False
  
  lCol = Cells(1, Columns.Count).End(xlToLeft).Column
  lRow = Cells.Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
  
  For c = 1 To lCol
  
    Heading = Cells(1, c).Value
    NextHeading = Cells(1, c + 1).Value
    
    If IsEmpty(Heading) And IsEmpty(NextHeading) Then
    Exit Sub
    End If
  
        If Heading = NextHeading Then
            StartCol = c
                      For R = 2 To lRow
                              If Heading = NextHeading Then
                              Cells(R, c) = Trim(Cells(R, c) & " " & Cells(R, c + 1))
                              End If
                      Next R
                      
                      EndCol = StartCol + 1
                      c = c - 1
                      Columns(EndCol).EntireColumn.Delete
              
          End If
    Next c


End Sub


I wrote this code and it works although its looping the values.
 
Upvote 0
for some strange reason when i use the V2 code i dont get that result.
Are you sure that you did not modify the code in any way to try to suit your individual circumstances?

Can you provide that sample data with XL2BB to ensure I am working with exactly the same sample data and layout as you?
Failing that, can you upload the sample file, with the v2 code included to DropBox or OneDrive/Google Drive etc and provide a shared link here?
 
Upvote 0

Forum statistics

Threads
1,223,632
Messages
6,173,472
Members
452,516
Latest member
archcalx

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