Solved

Custom List Sort for Large Spreadsheet

Posted on 2011-03-11
2
421 Views
Last Modified: 2012-05-11
I have a large spreadsheet containing stock data.  The file has 96 columns and becomes very time consuming when changing sorts from one column to another.  For example...of the 96 columns, I may want to sort ascending/descending values for 20 columns (with other columns containing supporting data).  Instead of scrolling to each column, I would prefer to create a dropdown list that will allow me to select a column header and sort it accordingly.  I have attached a trimmed down version that hopefully makes it easier to understand.  The custom list is in Cell A2.  Thanks in advance to anyone who can help out. SortList.xls
0
Comment
Question by:maverickcapital
2 Comments
 
LVL 30

Accepted Solution

by:
SiddharthRout earned 500 total points
ID: 35110457
Sample Attached. Hope it helps :)

Sid

Code Used

Dim i As Long, lastRow As Long
Dim aCell As Range, sortRange As Range
Dim strSearch As String
    
Sub Ascd()
    strSearch = ActiveSheet.Range("A2").Value
    
    If Len(Trim(strSearch)) = 0 Then
        MsgBox "Please select the header on which you want to sort"
    End If
    
    lastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
    
    Set sortRange = ActiveSheet.Range("A5:T" & lastRow)
    
    Set aCell = ActiveSheet.Rows(4).Find(What:=strSearch, LookIn:=xlValues, _
    LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False)
    
    If Not aCell Is Nothing Then
        sortRange.Sort Key1:=ActiveSheet.Range(Split(Cells(, aCell.Column).Address, "$")(1) & "5"), Order1:=xlAscending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortNormal
    Else
        MsgBox "Not Found"
    End If
End Sub

Sub Dscd()
    strSearch = ActiveSheet.Range("A2").Value
    
    If Len(Trim(strSearch)) = 0 Then
        MsgBox "Please select the header on which you want to sort"
    End If
    
    lastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
    
    Set sortRange = ActiveSheet.Range("A5:T" & lastRow)
    
    Set aCell = ActiveSheet.Rows(4).Find(What:=strSearch, LookIn:=xlValues, _
    LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False)
    
    If Not aCell Is Nothing Then
        sortRange.Sort Key1:=ActiveSheet.Range(Split(Cells(, aCell.Column).Address, "$")(1) & "5"), Order1:=xlDescending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortNormal
    Else
        MsgBox "Not Found"
    End If
End Sub

Open in new window

SortList.xls
0
 

Author Closing Comment

by:maverickcapital
ID: 35111689
you the man.  Works perfectly.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

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…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

744 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

11 Experts available now in Live!

Get 1:1 Help Now