If the data is transposed, use the following code
Sub Test()
For M = 2 To Cells(1, 1).CurrentRegion.Columns.Count
For N = 2 To Cells(1, 1).CurrentRegion.Rows.Count
If WorksheetFunction.CountIf(Sheets("Out").Columns(1), Cells(N, M).Value) = 0 Then
Sheets("Out").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0) = Cells(N, M).Value
End If
If Cells(N, M).Value <> "" Then
TargetRow = Sheets("Out").Columns(1).Find(Cells(N, M), , xlValues, xlWhole).Row
Sheets("Out").Cells(TargetRow, Columns.Count).End(xlToLeft).Offset(0, 1) = "'" & Cells(N, 1) & Cells(1, M)
End If
Next N
Next M
'Tidy up
MaxNumber = 30
Sheets("Out").Activate
Cells(2, 1).CurrentRegion.Sort Key1:=Cells(2, 1), order1:=xlAscending
For N = Cells(Rows.Count, 1).End(xlUp).Row To 2 Step -1
Do While Cells(N, Columns.Count).End(xlToLeft).Column > MaxNumber + 1
Rows(N + 1).Insert
Cells(N + 1, 1) = Cells(N, 1)
If (Cells(N, Columns.Count).End(xlToLeft).Column - 1) Mod MaxNumber > 0 Then
Range(Cells(N, Cells(N, Columns.Count).End(xlToLeft).Column + 1 - (Cells(N, Columns.Count).End(xlToLeft).Column - 1) Mod MaxNumber), Cells(N, Columns.Count)).Cut Destination:=Cells(N + 1, 2)
Else
Range(Cells(N, Cells(N, Columns.Count).End(xlToLeft).Column + 1 - MaxNumber), Cells(N, Columns.Count)).Cut Destination:=Cells(N + 1, 2)
End If
Loop
Next N
End Sub
Bookmarks