Sort By date with out selecting a cell or row

tony.reynolds

Board Regular
Joined
Jul 8, 2010
Messages
97
Im trying to sort my workbook by date and the code below works ok but it seems to have issues in some circumstances due to othe code conflickting

is there a better option to sort the sheet by the date values in column A:A

I also wanted it the remove or not show the sort button so the user can fidddle with the sorting


Code:
Sub SortByDate()
Sheets("Master Accounts").Activate
    Worksheets("Master Accounts").Unprotect (3221)
    Set ReturnCell = ActiveCell
    Application.ScreenUpdating = False
    
    Rows("4:4").Select
    Selection.AutoFilter
    ActiveWorkbook.Worksheets("Master Accounts").AutoFilter.Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Master Accounts").AutoFilter.Sort.SortFields.Add Key _
        :=Range("A4"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Master Accounts").AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Selection.AutoFilter
    Worksheets("Master Accounts").Protect (3221)
    
    ReturnCell.Activate
    Application.ScreenUpdating = True
    
End Sub

Ny help would be really appreciated
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Using Selection.AutoFilter toggles the autofilter. If it was on, it just got turned off.
Try this instead:
Code:
ActiveSheet.AutoFilterMode = False 'Ensures autofilter mode is off
Rows(4).AutoFilter 'Turns on autofilter for row 4
Excel 2007 and later can prevent sorting of a protected sheet.

What kind of conflicts are you talking about?
 
Upvote 0
hey that explains why when i run this macro it sometimes ended with an error as another macro had Selection.AutoFilter in it turning it on before this ran so this macro would have turned the filter of then tried to filter!

thanks a stack pbornemeier
 
Upvote 0

Forum statistics

Threads
1,224,609
Messages
6,179,876
Members
452,949
Latest member
Dupuhini

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