Macros to combine multiple rows into one

RSnap3232

New Member
Joined
Nov 20, 2020
Messages
43
Office Version
  1. 2016
  2. 2013
Platform
  1. Windows
Hello All

is there a macros that can be run to combine all data from multiple rows (which varies) depending on what is in column A as a reference. Using a work laptop so limited to what I can attach but please see below as an example, I want all the Routes for A1 to be on the same row
Route IDStartFromToEndLoad OriginLoad Dest
A1
17:15​
BristolGloucester
18:40​
BristolGloucester
A1
19:00​
GloucesterRyton
20:30​
GloucesterRyton
A1
22:00​
RytonBristol
00:00​
RytonBristol
A22
21:00​
BristolGloucester
22:10​
BristolGloucester
A22
23:00​
GloucesterSpitfire Road
00:10​
GloucesterSpitfire Road
A22
01:00​
Spitfire RoadRyton
01:45​
A22
03:45​
RytonBristol
06:00​
RytonBristol
A22
19:30​
BristolThames Valley
21:15​
BristolThames Valley
A4
23:15​
Thames ValleyBournemouth
00:45​
Thames ValleyBournemouth
A4
01:30​
BournemouthThames Valley
03:00​
BournemouthThames Valley
A4
03:45​
Thames ValleyBristol
05:30​
Thames ValleyBristol
C4
21:00​
BristolSwindon
22:00​
BristolSwindon
C4
22:30​
SwindonBristol
23:30​
SwindonBristol
C4
01:15​
BristolGloucester
02:15​
BristolGloucester
C4
02:45​
GloucesterBristol
03:45​
GloucesterBristol
C1
21:00​
BristolRyton
23:15​
BristolRyton
C1
03:30​
RytonBristol
05:45​
RytonBristol
C33
19:30​
BristolAPEC
19:45​
C33
20:30​
APECRyton
22:30​
APECRyton
C33
01:20​
RytonBristol
03:35​
RytonBristol
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
This macro will place the result in Sheet2. Change the sheet names (in red) to suit your needs.
Rich (BB code):
Sub CombineRows()
    Application.ScreenUpdating = False
    Dim LastRow As Long, srcWS As Worksheet, desWS As Worksheet, i As Long, v As Variant, rng As Range, x As Long: x = 2
    Set srcWS = Sheets("Sheet1")
    Set desWS = Sheets("Sheet2")
    v = srcWS.Range("A2", srcWS.Range("A" & Rows.Count).End(xlUp)).Value
    With CreateObject("Scripting.Dictionary")
        For i = 1 To UBound(v, 1)
            If Not .Exists(v(i, 1)) Then
                .Add v(i, 1), Nothing
                With srcWS
                    .Range("A1").CurrentRegion.AutoFilter 1, v(i, 1)
                    desWS.Cells(desWS.Rows.Count, "A").End(xlUp).Offset(1) = v(i, 1)
                    For Each rng In .Range("B2", .Range("B" & .Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible)
                        With desWS
                            rng.Resize(, 6).Copy .Cells(x, .Columns.Count).End(xlToLeft).Offset(, 1)
                        End With
                    Next rng
                    x = x + 1
                End With
            End If
        Next i
    End With
    desWS.Columns.AutoFit
    srcWS.Range("A1").AutoFilter
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
This macro will place the result in Sheet2. Change the sheet names (in red) to suit your needs.
Rich (BB code):
Sub CombineRows()
    Application.ScreenUpdating = False
    Dim LastRow As Long, srcWS As Worksheet, desWS As Worksheet, i As Long, v As Variant, rng As Range, x As Long: x = 2
    Set srcWS = Sheets("Sheet1")
    Set desWS = Sheets("Sheet2")
    v = srcWS.Range("A2", srcWS.Range("A" & Rows.Count).End(xlUp)).Value
    With CreateObject("Scripting.Dictionary")
        For i = 1 To UBound(v, 1)
            If Not .Exists(v(i, 1)) Then
                .Add v(i, 1), Nothing
                With srcWS
                    .Range("A1").CurrentRegion.AutoFilter 1, v(i, 1)
                    desWS.Cells(desWS.Rows.Count, "A").End(xlUp).Offset(1) = v(i, 1)
                    For Each rng In .Range("B2", .Range("B" & .Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible)
                        With desWS
                            rng.Resize(, 6).Copy .Cells(x, .Columns.Count).End(xlToLeft).Offset(, 1)
                        End With
                    Next rng
                    x = x + 1
                End With
            End If
        Next i
    End With
    desWS.Columns.AutoFit
    srcWS.Range("A1").AutoFilter
    Application.ScreenUpdating = True
End Sub
hello mumps sorry about the delay but that is brilliant thank you, what would I need to change if I need to include columns H, I and J as Ive been forced to add additional columns
 
Upvote 0
Here is another option that you could try. This operates on the active sheet and moves/removes data so test with a copy of your worksheet.

VBA Code:
Sub Combine_Rows()
  Dim r As Long
 
  Application.ScreenUpdating = False
  For r = Range("A" & Rows.Count).End(xlUp).Row To 3 Step -1
    If Range("A" & r).Value = Range("A" & r - 1).Value Then
      Range("B" & r, Range("A" & r).End(xlToRight)).Copy Cells(r - 1, Columns.Count).End(xlToLeft).Offset(, 1)
      Rows(r).Delete
    End If
  Next r
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
I think that Peter may have given you the solution.
 
Upvote 0
Here is another option that you could try. This operates on the active sheet and moves/removes data so test with a copy of your worksheet.

VBA Code:
Sub Combine_Rows()
  Dim r As Long
 
  Application.ScreenUpdating = False
  For r = Range("A" & Rows.Count).End(xlUp).Row To 3 Step -1
    If Range("A" & r).Value = Range("A" & r - 1).Value Then
      Range("B" & r, Range("A" & r).End(xlToRight)).Copy Cells(r - 1, Columns.Count).End(xlToLeft).Offset(, 1)
      Rows(r).Delete
    End If
  Next r
  Application.ScreenUpdating = True
End Sub
hello Peter, that has worked a treat thank you.
 
Upvote 0
You're welcome. Glad we could help. Thanks for the follow-up. :)
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

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