Sorting Issue

zsjoy

New Member
Joined
Mar 25, 2009
Messages
4
I have a workbook with several worksheets. I add new sheets to the workbook often. The sheets contain the same type of data, but with different dates, amounts, vendors, etc. After I enter the new data, I will need to sort according to certain criteria. I am fine with this step. What is aggravating me in 2007 version is that I have to redefine the Sort By and the Then By for each worksheet. In the 2003 version, this defaulted for the woorkbook that I would be using. Is there a way to save the sort procedure so I don't have to redefine how to sort each time? I tried to record a macro (a process I am not very familiar with), but it appears it only works on the worksheet that it was created for. Also, the number of rows changes with each worksheet.
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Your recorded macro should look something like

Rich (BB code):
Sub Macro5()
'
' Macro5 Macro
'
'
    ActiveWorkbook.Worksheets("Summary").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Summary").Sort.SortFields.Add Key:=Range( _
        "C2:C1902"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("Summary").Sort.SortFields.Add Key:=Range( _
        "E2:E1902"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Summary").Sort
        .SetRange Range("A1:E1902")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub

Please create a backup copy of your workbook before trying this.

Using your own recorded macro, not this one, replace each occurence of "ActiveWorkbook.Worksheets("your sheet name")" with "ActiveSheet", this should then allow you to use it with any sheet.

Note that if the number of rows in your sheets varies then some additional editing will be needed to allow for this.

Hope this helps
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,684
Members
448,977
Latest member
dbonilla0331

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top