+ Reply to Thread
Results 1 to 6 of 6

Help needed with an auto sort macro!

Hybrid View

  1. #1
    Registered User
    Join Date
    07-25-2012
    Location
    Liverpool, England
    MS-Off Ver
    Excel 2010
    Posts
    3

    Help needed with an auto sort macro!

    Hi

    I need some help please. Although I have used excel for years, I have never actually used it in-depth so am a novice sorry.

    I have put together a spreadsheet containing all the results of my teams bowling results for this season which I have finally got working correctly. The results then feed into another sheet to produce a league table.

    My problem is that I have tried to make a macro to autosort the table whenever something is changed in the resluts sheet. The macro works great if I use the shortcut key I put in but it doesnt do it automatically which is what I want but dont know how to do.

    My macro is


    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Macro8()
    '
    ' Macro8 Macro
    '
    
    '
        Range("A5:Y28").Select
        ActiveWorkbook.Worksheets("Averages").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Averages").Sort.SortFields.Add Key:=Range("E5:E28" _
            ), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
        ActiveWorkbook.Worksheets("Averages").Sort.SortFields.Add Key:=Range("I5:I28" _
            ), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("Averages").Sort
            .SetRange Range("A5:Y28")
            .Header = xlGuess
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    End Sub

    Can anybody help me please??

    I have attached my spreadsheet also
    Attached Files Attached Files
    Last edited by Cutter; 07-25-2012 at 08:46 PM. Reason: Added code tags

  2. #2
    Forum Contributor
    Join Date
    10-08-2010
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    386

    Re: Help needed with an auto sort macro!

    When you are in your VBA editor (ALT + F11) Double click Sheet1 (Results) on the left hand side
    and enter the code below. Let me know


    Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    
    Application.ScreenUpdating = False
    
    Sheets("Averages").Activate
        
        ActiveWorkbook.Worksheets("Averages").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Averages").Sort.SortFields.Add Key:=Range("E5:E28" _
            ), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
        ActiveWorkbook.Worksheets("Averages").Sort.SortFields.Add Key:=Range("I5:I28" _
            ), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("Averages").Sort
            .SetRange Range("A5:Y28")
            .Header = xlGuess
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    Sheets("Results").Activate
    
    End Sub

  3. #3
    Registered User
    Join Date
    07-25-2012
    Location
    Liverpool, England
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Help needed with an auto sort macro!

    Brilliant thank you!!! Were was i going wrong by the way?

  4. #4
    Forum Contributor
    Join Date
    10-08-2010
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    386

    Re: Help needed with an auto sort macro!

    For me the range selection was messing it up. I just took that out, I dont even think i needed to put the sheet.select function in there. But hey if it works don't change it right?

  5. #5
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: Help needed with an auto sort macro!

    @ Colin Strowbridge

    Welcome to the forum.

    Based on your last post it seems that you are satisfied with the solution(s) you've received but you haven't marked your thread as SOLVED. I'll do that for you now but please keep in mind for your future threads that Rule #9 requires you to do that yourself. If your problem has not been solved you can use Thread Tools (located above your first post) and choose "Mark this thread as unsolved".
    Thanks.

    Also, as a new member of the forum, you may not be aware that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of those who helped.

  6. #6
    Registered User
    Join Date
    07-25-2012
    Location
    Liverpool, England
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Help needed with an auto sort macro!

    Thank you for the advice and all done!

+ 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