+ Reply to Thread
Results 1 to 5 of 5

Sort Macro Help

Hybrid View

  1. #1
    Registered User
    Join Date
    03-03-2010
    Location
    Orlando, Florida
    MS-Off Ver
    Excel 2007
    Posts
    48

    Sort Macro Help

    I'm working in a file with multiple tables on each worksheet. Basically, there is a template worksheet that I copy every day and update with new daily values. There are three tables down (each with 20 rows and 10 columns) and four across. What I'm trying to do is create a macro that sorts each table by the rightmost column. I can record the macro just fine, but the problem is the copying of the template worksheet everyday. The macro only references the that worksheet. Is there a way to edit the macro code to execute only on the active worksheet? Could there be anything else that I'm missing here? Thanks for any help.

  2. #2
    Forum Contributor colofnature's Avatar
    Join Date
    05-11-2006
    Location
    -
    MS-Off Ver
    -
    Posts
    301

    Re: Sort Macro Help

    Change references to the sheet you recorded on [e.g. Sheets("Sheet1")] to "ActiveSheet" in the macro, that way whatever sheet you start on when you run it will have the actions applied to it.
    If you give someone a program, you will frustrate them for a day; if you teach them how to program, you will frustrate them for a lifetime.

  3. #3
    Registered User
    Join Date
    03-03-2010
    Location
    Orlando, Florida
    MS-Off Ver
    Excel 2007
    Posts
    48

    Re: Sort Macro Help

    My code doesn't have the exact same reference as you'll see below, but I went in and changed "template" to "ActiveSheet" and it caused an error. Below is the code for the first of 12.

    Sub Sorter()
    '
    ' Sorter Macro
    '
    ' Keyboard Shortcut: Ctrl+Shift+Q
    '
    Range("A3:J22").Select
    ActiveWorkbook.Worksheets("template").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("template").Sort.SortFields.Add Key:=Range( _
    "J3:J22"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
    xlSortNormal
    ActiveWorkbook.Worksheets("template").Sort.SortFields.Add Key:=Range( _
    "A3:A22"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
    xlSortNormal
    With ActiveWorkbook.Worksheets("template").Sort
    .SetRange Range("A2:J22")
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
    End With
    End Sub

  4. #4
    Forum Contributor colofnature's Avatar
    Join Date
    05-11-2006
    Location
    -
    MS-Off Ver
    -
    Posts
    301

    Re: Sort Macro Help

    Change all the lines starting with "ActiveWorkbook.Worksheets("template")" so that they start with "ActiveSheet" instead.

    For example:
    ActiveWorkbook.Worksheets("template").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("template").Sort.SortFields.Add Key:=Range( _
    "J3:J22"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
    xlSortNormal

    is changed to:

    ActiveSheet.Sort.SortFields.Clear
    ActiveSheet.Sort.SortFields.Add Key:=Range( _
    "J3:J22"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
    xlSortNormal
    ' And so on...

  5. #5
    Registered User
    Join Date
    03-03-2010
    Location
    Orlando, Florida
    MS-Off Ver
    Excel 2007
    Posts
    48

    Re: Sort Macro Help

    Perfect. Thanks a lot. It works exactly how I need it to.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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