Defining Sort Order in Excel Template

G

Guest

I am creating Excel Template. Template has first 4 rows reserved for report
header. I would like to sort rest of the report to be sorted on Column-A,
then on Column-Bm and then on Column-F.

How do I define that sort order in template itself so that any time when
somebody generates report and opens that with this template it is
automatically sorted based on the order defined in template.
 
D

Dave Peterson

I think I would put a dedicated macro that sorted exactly the way I wanted.
Then I'd drop a button from the Forms toolbar on that worksheet--and assign the
macro to that button.

Then the users can still sort by whatever they want (via Data|Sort).

Option Explicit
Sub mySpecialSort()

Dim myRng As Range

With ActiveSheet
Set myRng = .Range("A5:X" & .Cells(.Rows.Count, "A").End(xlUp).Row)
End With

With myRng
.Cells.Sort key1:=.Columns(1), order1:=xlAscending, _
key2:=.Columns(2), order2:=xlAscending, _
key3:=.Columns(6), order3:=xlAscending, _
header:=xlYes
End With

End Sub


I sorted columns A:X--change that if you need to. And I assumed that row 5 had
headers. Change xlyes to xlno if it doesn't.

If you're new to macros, you may want to read David McRitchie's intro at:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top