+ Reply to Thread
Results 1 to 7 of 7

Help sorting data

Hybrid View

  1. #1
    Registered User
    Join Date
    03-08-2010
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    11

    Help sorting data

    I want to make a button which when clicked will sort a data set (ascending) depending on which column was selected - is anyone able to share the code required to make this work?

  2. #2
    Forum Expert
    Join Date
    11-23-2005
    Location
    Rome
    MS-Off Ver
    Ms Office 2016
    Posts
    1,628

    Re: Help sorting data

    Try with this macro:
    Sub Macro1()
      myCol = Selection.Column
        
      With ThisWorkbook.ActiveSheet
         .Cells.Sort Key1:=.Cells(1, myCol), Order1:=xlAscending, _
             Header:=xlYes, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
             DataOption1:=xlSortNormal
      End With
    End Sub
    Regards,
    Antronio

  3. #3
    Registered User
    Join Date
    03-08-2010
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Help sorting data

    That is great thanks, forgive my 'green-ness' but is there away to define the data range? I would like to keep some of the bottom of the table rows constant.....

  4. #4
    Forum Expert
    Join Date
    11-23-2005
    Location
    Rome
    MS-Off Ver
    Ms Office 2016
    Posts
    1,628

    Re: Help sorting data

    Yes, you could use this code:
    Sub Macro1()
      Dim myCol As Integer, myRange As String
      
      myCol = Selection.Column
      myRange = "a1:f100"
        
      With ThisWorkbook.ActiveSheet
         .Range(myRange).Sort Key1:=.Cells(1, myCol), Order1:=xlAscending, _
             Header:=xlYes, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
             DataOption1:=xlSortNormal
      End With
    End Sub
    Regards,
    Antonio

  5. #5
    Registered User
    Join Date
    03-08-2010
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Help sorting data

    Perfect thank you Antonio.

  6. #6
    Registered User
    Join Date
    03-08-2010
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Help sorting data

    Final question on this topic - is there a way to reset the sorting so the table is back to the original display?

  7. #7
    Registered User
    Join Date
    03-08-2010
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Help sorting data

    actually - will add another column and with a number rank and just sort that one.....

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1