Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Macro to sort monthly lists across the months

Posted on 2014-12-22
5
Medium Priority
?
58 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 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
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…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

715 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