Solved

Macro to sort monthly lists across the months

Posted on 2014-12-22
5
51 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
Comment Utility
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
Comment Utility
How about a macro which transposes the data and then sets up the PT?
0
 
LVL 27

Expert Comment

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

Accepted Solution

by:
Glenn Ray earned 500 total points
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

771 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now