VBA/macro/code for combined the name from multiple row

Vishaal

Well-known Member
Joined
Mar 16, 2019
Messages
533
Office Version
  1. 2010
  2. 2007
Platform
  1. Windows
  2. Web
HI All,

Thanks in advance, we have the following sheet in excel and there is names in two or three rows, we want to combine them.

For example Code No. 763 (row 10) name in three rows, we want to make in one "UGC-Net/JRF/SET Teaching, Research, practice, numerical, 100 mock Aptitude (General Paper-1)"

We have many names this type in file. help pls.

Excel 2010 32 bit
B
C
2
Code​
Name
3
420​
U.G.C. NET/JRF/SET Teaching & Research
4
Aptitude (General Paper-I)
5
1553​
UGC-Net/JRF/SET Teaching & Research Aptitude (General Paper-1)
6
1588​
CSIR-UGC NET/JRF/SET Chemical Sciences
7
500​
CSIR-UGC NET/JRF/SET Physical Sciences
8
1551​
CSIR-UGC NET/JRF/SET Physical Sciences
9
1587​
CSIR-UGC NET/JRF/SLET Mathematical Sciences
10
763​
UGC-Net/JRF/SET Teaching, Research,
11
practice, numerical, 100 mock
12
Aptitude (General Paper-1)
13
894​
UGC NET/JRF/SET Computer Science and Applications (Paper II)
14
931​
UGC-NET/JRF/SLET Physical Education (Paper II)
15
317​
CSIR-UGC NET/JRF/SET Life Sciences
Sheet: Sheet1
 
Another vba option that might be useful if the data set is very large.
This code writes the results in columns D:E but if you want to replace the original data, just remove the .Offset(, 2) near the end.

Rich (BB code):
Sub CombineRows()
  Dim a As Variant, b As Variant
  Dim i As Long, k As Long
  
  With Range("B3", Range("C" & Rows.Count).End(xlUp))
    a = .Value
    ReDim b(1 To UBound(a), 1 To 2)
    For i = 1 To UBound(a)
      If Len(a(i, 1)) > 0 Then
        k = k + 1
        b(k, 1) = a(i, 1)
        b(k, 2) = a(i, 2)
      Else
        b(k, 2) = b(k, 2) & " " & a(i, 2)
      End If
    Next i
    .Offset(, 2).Value = b
  End With
End Sub
 
Upvote 0

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Thanks lrobbo314, but we are using the excel 2010 and have no idea about power query


You can do this with Power Query. Copy paste didn't work with your OP, so I used fake data. Should work just the same.

Book1
ABCDE
1CodeNameCodeName
2AABCAABC 123
3123BX
4BXCCDE 345
5CCDE
6345
Sheet5


Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
    ToText = Table.TransformColumnTypes(Source,{{"Code", type text}, {"Name", type text}}),
    FilledDown = Table.FillDown(ToText,{"Code"}),
    Group = Table.Group(FilledDown, {"Code"}, {{"Name", each _, type table [Code=text, Name=anynonnull]}}),
    Combine = Table.TransformColumns(Group,{"Name", each Text.Combine(Table.Column(_,"Name")," ")})
in
    Combine
 
Upvote 0
Thanks sandy666, we are using excel 2010 and have no idea about power query

maybe an alternative M-code ?
CodeNameCodeName
420U.G.C. NET/JRF/SET Teaching & Research420U.G.C. NET/JRF/SET Teaching & Research Aptitude (General Paper-I)
Aptitude (General Paper-I)1553UGC-Net/JRF/SET Teaching & Research Aptitude (General Paper-1)
1553UGC-Net/JRF/SET Teaching & Research Aptitude (General Paper-1)1588CSIR-UGC NET/JRF/SET Chemical Sciences
1588CSIR-UGC NET/JRF/SET Chemical Sciences500CSIR-UGC NET/JRF/SET Physical Sciences
500CSIR-UGC NET/JRF/SET Physical Sciences1551CSIR-UGC NET/JRF/SET Physical Sciences
1551CSIR-UGC NET/JRF/SET Physical Sciences1587CSIR-UGC NET/JRF/SLET Mathematical Sciences
1587CSIR-UGC NET/JRF/SLET Mathematical Sciences763UGC-Net/JRF/SET Teaching, Research, practice, numerical, 100 mock Aptitude (General Paper-1)
763UGC-Net/JRF/SET Teaching, Research,894UGC NET/JRF/SET Computer Science and Applications (Paper II)
practice, numerical, 100 mock931UGC-NET/JRF/SLET Physical Education (Paper II)
Aptitude (General Paper-1)317CSIR-UGC NET/JRF/SET Life Sciences
894UGC NET/JRF/SET Computer Science and Applications (Paper II)
931UGC-NET/JRF/SLET Physical Education (Paper II)
317CSIR-UGC NET/JRF/SET Life Sciences

Code:
// Table1
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Group = Table.Group(Table.FillDown(Source,{"Code"}), {"Code"}, {{"Count", each _, type table}}),
    List = Table.AddColumn(Group, "Name", each Table.Column([Count],"Name")),
    Extract = Table.TransformColumns(List, {"Name", each Text.Combine(List.Transform(_, Text.From), " "), type text})
in
    Extract
 
Upvote 0
Thanks Peter_SSs,

We have tried but code runs and copy the same data to right side in excel

Another vba option that might be useful if the data set is very large.
This code writes the results in columns D:E but if you want to replace the original data, just remove the .Offset(, 2) near the end.

Rich (BB code):
Sub CombineRows()
  Dim a As Variant, b As Variant
  Dim i As Long, k As Long
 
  With Range("B3", Range("C" & Rows.Count).End(xlUp))
    a = .Value
    ReDim b(1 To UBound(a), 1 To 2)
    For i = 1 To UBound(a)
      If Len(a(i, 1)) > 0 Then
        k = k + 1
        b(k, 1) = a(i, 1)
        b(k, 2) = a(i, 2)
      Else
        b(k, 2) = b(k, 2) & " " & a(i, 2)
      End If
    Next i
    .Offset(, 2).Value = b
  End With
End Sub
 
Upvote 0
We have tried but code runs and copy the same data to right side in excel
So the cells in column B that appear empty in your sample data are not in fact empty. Try this instead.

Rich (BB code):
If Len(a(i, 1)) > 0 Then
If Len(a(i, 1)) > 1 Then
 
Upvote 0

Forum statistics

Threads
1,214,948
Messages
6,122,420
Members
449,083
Latest member
Ava19

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