Solved

Macro to sort monthly lists across the months

Posted on 2014-12-22
5
54 Views
Last Modified: 2014-12-23
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.
Sort-across-months.xlsx
0
Comment
Question by:Saqib Husain, Syed
  • 3
  • 2
5 Comments
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40513477
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.

Regards,
-Glenn
EE-Sort-across-months.xlsx
0
 
LVL 43

Author Comment

by:Saqib Husain, Syed
ID: 40513494
How about a macro which transposes the data and then sets up the PT?
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40513921
That is certainly possible.  I'll try to have a solution shortly.
0
 
LVL 27

Accepted Solution

by:
Glenn Ray earned 500 total points
ID: 40514031
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("Scores").Delete
    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
    Sheets(1).Select
    Range("A2").Select
    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
        Loop
        ActiveCell.Offset(0, 1).Select
    Loop
    
    Sheets("Scores").Select
    Range("A1:C1").EntireColumn.AutoFit
    Range("A2").Select
    
    'Add PivotTable
    Sheets("Summary").Delete
    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 _
        :=xlPivotTableVersion14
    'Sheets("Summary").Select
    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
    Range("A2").Select
    
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    
End Sub

Open in new window


Modified workbook attached.

Regards,
-Glenn
EE-Sort-across-months.xlsm
0
 
LVL 43

Author Closing Comment

by:Saqib Husain, Syed
ID: 40514761
Great, Thanks.

I only had to delete these arguments from the code to be able to use it in 2007
DefaultVersion :=xlPivotTableVersion14
Version:=xlPivotTableVersion14
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvieā€¦
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question