Macro to sort monthly lists across the months

I have this spreadsheet showing monthly lists copied side-by-side showing names and scores.

I need a macro which would sort the names such that the names are aligned in rows which can be used to sum the scores for every person horizontally.

Not a perfectly worded explanation but I think the file can show what I mean.
LVL 43
Saqib Husain, SyedEngineerAsked:
Who is Participating?
Glenn RayConnect With a Mentor Excel VBA DeveloperCommented:
This code will create a transposed data table and then create a PivotTable that sums up the scores for all names listed.  It will overwrite any pre-existing Scores or Summary sheets.
Option Explicit
Sub Transpose_To_Table()
    Dim dtMonth As Date
    Dim r, c As Integer
    Dim r2 As Integer
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    On Error Resume Next
    Sheets.Add after:=Sheets(1)
    ActiveSheet.Name = "Scores"
    Range("A1").Value = "Date"
    Range("B1").Value = "Name"
    Range("C1").Value = "Score"
    r2 = 2
    'Create Transposed Scores Table/sheet
    Do Until ActiveCell.Value = ""
        dtMonth = ActiveCell.Value
        c = ActiveCell.Column
        r = 3
        Do Until Cells(r, c).Value = ""
            Sheets("Scores").Cells(r2, 1).Value = dtMonth
            Sheets("Scores").Cells(r2, 2).Value = Cells(r, c).Value
            Sheets("Scores").Cells(r2, 3).Value = Cells(r, c + 1).Value
            r2 = r2 + 1
            r = r + 1
        ActiveCell.Offset(0, 1).Select
    'Add PivotTable
    Sheets.Add after:=Sheets("Scores")
    ActiveSheet.Name = "Summary"
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "Scores!R1C1:R" & r2 - 1 & "C3", Version:=xlPivotTableVersion14).CreatePivotTable _
        TableDestination:="Summary!R3C1", TableName:="PivotTable1", DefaultVersion _
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Name")
        .Orientation = xlRowField
        .Position = 1
    End With
    ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
        "PivotTable1").PivotFields("Score"), "Sum of Score", xlSum
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
End Sub

Open in new window

Modified workbook attached.

Glenn RayExcel VBA DeveloperCommented:
A macro could certainly be written, but it seems problematic in that a row would have to be dedicated to each unique name that shows up, leaving a lot of unused space and making it difficult to read.

Instead, would it be possible to consider transposing the data into a table so that you could then run a PivotTable summary on the results?  It would offer more analytical capabilities on the data and keep the source data intact.

See the example workbook.  I've tranpsosed your data into such a table.  If you have a significantly larger set of data, a macro could be written to achieve the same results (i.e., tranpose the data into this format).  Future months' data could be easily appended to the data table and only the month date would need to be added to the first column.

Saqib Husain, SyedEngineerAuthor Commented:
How about a macro which transposes the data and then sets up the PT?
Glenn RayExcel VBA DeveloperCommented:
That is certainly possible.  I'll try to have a solution shortly.
Saqib Husain, SyedEngineerAuthor Commented:
Great, Thanks.

I only had to delete these arguments from the code to be able to use it in 2007
DefaultVersion :=xlPivotTableVersion14
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.