I don't think you even need the worksheet1. Just make a copy of worksheet 2 and run this macro:
Option Explicit
Sub Consolidate()
'JBeaucaire (9/18/2009)
'Columnar data is Sorted/Matched by column A values, merge all other cells into row format
Dim LastRow As Long, NextCol As Long
Dim LastCol As Long, Rw As Long, Cnt As Long
Dim delRNG As Range
Application.ScreenUpdating = False
'Remove blank rows
Range("A1:A" & Rows.Count).SpecialCells(xlBlanks).EntireRow.Delete xlShiftUp
'Sort data
LastRow = Range("A" & Rows.Count).End(xlUp).Row
Range("A1").CurrentRegion.Sort Key1:=Range("B2"), Order1:=xlAscending, Header:=xlYes
'Seed the delete range
Set delRNG = Range("A" & LastRow + 10)
'Group matching names
For Rw = LastRow To 2 Step -1
If Cells(Rw, "A").Value = Cells(Rw - 1, "A").Value Then
Range(Cells(Rw, "C"), Cells(Rw, Columns.Count).End(xlToLeft)).Copy _
Cells(Rw - 1, Columns.Count).End(xlToLeft).Offset(0, 1)
Set delRNG = Union(delRNG, Range("A" & Rw))
End If
Next Rw
'Delete unneeded rows all at once
delRNG.EntireRow.Delete (xlShiftUp)
Set delRNG = Nothing
'Add titles
LastCol = Cells(1, 1).CurrentRegion.Columns.Count
With Range("C1", Cells(1, LastCol))
.FormulaR1C1 = "=""Number "" & COLUMN(RC[-2])"
.Value = .Value
End With
Cells.Columns.AutoFit
Application.ScreenUpdating = True
End Sub
Option Explicit
Sub Consolidate()
'JBeaucaire (9/18/2009)
'Columnar data is Sorted/Matched by column A values, merge all other cells into row format
Dim LastRow As Long, NextCol As Long
Dim LastCol As Long, Rw As Long, Cnt As Long
Dim delRNG As Range
Application.ScreenUpdating = False
'Remove blank rows
Range("A1:A" & Rows.Count).SpecialCells(xlBlanks).EntireRow.Delete xlShiftUp
'Sort data
LastRow = Range("A" & Rows.Count).End(xlUp).Row
Range("A1").CurrentRegion.Sort Key1:=Range("B2"), Order1:=xlAscending, Header:=xlYes
'Seed the delete range
Set delRNG = Range("A" & LastRow + 10)
'Group matching names
For Rw = LastRow To 2 Step -1
If Cells(Rw, "A").Value = Cells(Rw - 1, "A").Value Then
Range(Cells(Rw, "C"), Cells(Rw, Columns.Count).End(xlToLeft)).Copy _
Cells(Rw - 1, Columns.Count).End(xlToLeft).Offset(0, 1)
Set delRNG = Union(delRNG, Range("A" & Rw))
End If
Next Rw
'Delete unneeded rows all at once
delRNG.EntireRow.Delete (xlShiftUp)
Set delRNG = Nothing
'Add titles
LastCol = Cells(1, 1).CurrentRegion.Columns.Count
With Range("C1", Cells(1, LastCol))
.FormulaR1C1 = "=""Number "" & COLUMN(RC[-2])"
.Value = .Value
End With
Cells.Columns.AutoFit
Application.ScreenUpdating = True
End Sub
How/Where to install the macro:
1. Open up your workbook
2. Get into VB Editor (Press Alt+F11)
3. Insert a new module (Insert > Module)
4. Copy and Paste in your code (given above)
5. Get out of VBA (Press Alt+Q)
6. Save as a macro-enabled workbook
The macro is installed and ready to use. Press Alt-F8 and select it from the macro list.
Bookmarks