Merge cells in Column B based on sells in column A

timspin

Board Regular
Joined
Nov 18, 2002
Messages
231
Hi All

I have looked through the archive and cant find a solution to my problem - hope someone can help.

I have two columns of data (a few thousand rows), I want to merge all the table names into 1 cell for each variable name. Each variable has a varying number of tables

Row...Column A.....................Column B
1.......Variable Name..............Table Name
2.......account_number...........auto_commercial_vehicle_t
3.........................................auto_covered_symbol_t
4.........................................auto_transaction_details_t
5.........................................coverage_summary_t
6.........................................nb_summary_t
7.........................................policy_summary_t
8.........................................transaction_details_t
9........acct_num...................opp_smry_t
10.......................................pol_t
11......annl_sale_amt.............opp_smry_t
12.......................................pol_t
13......annual_sale_amount....nb_summary_t
14.......................................policy_summary_t
15......app_sys_cde...............pol_t

So for the table above I would see cell a2 'account_number' and cell b2 a list of the associated table names (in a list separated by new lines, or carriage returns)
My ideal results would look like this, with 1 row per variable name.

Row....Column A.................Column B

1........Variable Name.........Table Name
2........account_number......auto_commercial_vehicle_t
.......................................auto_covered_symbol_t
.......................................auto_transaction_details_t
.......................................coverage_summary_t
.......................................nb_summary_t
.......................................policy_summary_t
.......................................transaction_details_t
3........acct_num................opp_smry_t
.......................................pol_t
4........annl_sale_amt.........opp_smry_t
.......................................pol_t

Note: My data comes from a pivot table so I could start with each variable name for each table like this, if that helps the end solution.

Row....Column A....................Column B

1........Variable Name.............Table Name
2........account_number..........auto_commercial_vehicle_t
3........account_number..........auto_covered_symbol_t
4........account_number..........auto_transaction_details_t
5........account_number..........coverage_summary_t
6........account_number..........nb_summary_t
7........account_number..........policy_summary_t
8........account_number..........transaction_details_t

Thanks all.
Tim
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Try this.
the result will be in columns C and D

Code:
Sub Merge_Cells()


    Dim i As Double, j As Double, u As Double
    j = 1
    Range("D2:E" & Rows.Count).Clear
    For i = 2 To Range("B" & Rows.Count).End(xlUp).Row
        If Cells(i, "A").Value = "" Then
            Cells(j, "E").Value = Cells(j, "E").Value & Chr(10) & Cells(i, "B").Value
        Else
            j = j + 1
            Cells(j, "D").Value = Cells(i, "A").Value
            Cells(j, "E").Value = Cells(i, "B").Value
         End If
    Next
    
    u = Range("D" & Rows.Count).End(xlUp).Row
    Range("D2:E" & u).VerticalAlignment = xlTop
    Rows("2:" & u).EntireRow.AutoFit


End Sub
 
Upvote 0
Hi Dante

I copied and pasted this into the VB editor and ran it (straight from editor and from Macro menu) - got error 'Run time error 16 Expression to complex' when I debug it highlights this row - For i = 2 To Range("B" & Rows.Count).End(xlUp).Row

Thanks
Tim
 
Upvote 0
Use this:

Code:
Sub Merge_Cells()


    Dim i As Double, j As Double, u As Double
    
    Application.ScreenUpdating = False
    
    j = 1
    Range("D2:E" & Rows.Count).Clear
    u = Range("B" & Rows.Count).End(xlUp).Row
    For i = 2 To u
        If Cells(i, "A").Value = "" Then
            Cells(j, "E").Value = Cells(j, "E").Value & Chr(10) & Cells(i, "B").Value
        Else
            j = j + 1
            Cells(j, "D").Value = Cells(i, "A").Value
            Cells(j, "E").Value = Cells(i, "B").Value
         End If
    Next
    
    u = Range("D" & Rows.Count).End(xlUp).Row
    Range("D2:E" & u).VerticalAlignment = xlTop
    Rows("2:" & u).EntireRow.AutoFit


    Application.ScreenUpdating = True
    
    MsgBox "End"
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,088
Messages
6,128,744
Members
449,466
Latest member
Peter Juhnke

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