Solved

Macro to sort monthly lists across the months

Posted on 2014-12-22
5
55 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

756 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