Solved

Custom List Sort for Large Spreadsheet

Posted on 2011-03-11
2
423 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
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.

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