LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 576
Default Dynamically set Sort keys

Wilbur,

Just realized - it is not necessary to select to do the sort...
Range("a22:U554").Sort.......................

And there are ways to make the range dynamic (if needed)...
--
sb
"Wilbur" wrote in message
...
Thanks Tom, your suggestions gave me the direction I needed to get this
wrapped up. To close the loop I'm posting my code in the hope that it'll

be
useful to a future searcher. Portions of this code have been contributed

or
drawn from other newsgroup postings...thanks to all that have helped..

The project has VB code that enables the selection of sort keys and
subsequent sorting of a database by selecting the sort keys from in cell
drop downs. this makes it possible to sort the database without having to
go through the normal excel menu structue. For whatever reason some of my
more novice co-workers can't seem to grasp the built in sorting functions
and selection of sort ranges :)

The project has two VB components. The first is this section of code

placed
in the sheet code.

Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
If Not Intersect(Target, Range("e20")) Is Nothing Then threekeysort
End Sub

This code will run the "threekeysort" macro anytime cell e20 is selected.

The second component is the sorting subroutine named threekeysort. I

placed
this in a regular code module. In a nut shell, this uses in cell lists
(using the menu command for Data:Validation). There are dropdowns in

cells
B20, C20, D20, populated with values in cells A21:U21. All of this is at
the worksheet level, no VB.

The macro to tie it all together follows. I've tried to comment the code

to
make it readable. I hope it helps.

Sub threekeysort()

' The Match function returns the index value of the item in range a15:u15
(field names)
' that match the value in b14, c14, or d14

my_sort_key1 = WorksheetFunction.Match(Range("b20"), Range("A21:u21"), 0)
my_sort_key2 = WorksheetFunction.Match(Range("c20"), Range("A21:u21"), 0)
my_sort_key3 = WorksheetFunction.Match(Range("d20"), Range("A21:u21"), 0)

' Turn off screen updating while the macro runs to give smoother updates
Application.ScreenUpdating = False



' Set the sort range, then perform the sort on the field values identified
by the three index keys
' The sort key is set as the column indicated by the result of
columns(my_sort_key1)

Range("a22:U554").Select
Selection.Sort key1:=Columns(my_sort_key1), Order1:=xlAscending,
key2:=Columns(my_sort_key2) _
, Order2:=xlAscending, key3:=Columns(my_sort_key3),
Order3:=xlAscending, Header:= _
xlGuess, OrderCustom:=1, MatchCase:=False,
Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal, DataOption2:=xlSortNormal,

DataOption3:=
_
xlSortNormal

' Select the first sort key cell to deselect the entire sort range. this
gives a cleaner appearnace rather
' than keeping the entire sort range selected following the sort

Range("b20").Select

' Turn on screen updating

Application.ScreenUpdating = True

End Sub






 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sort worksheet on multiple keys - Primary, secondary, tirterary, etc. Doug Mc New Users to Excel 12 October 22nd 09 02:18 AM
Many Sort Keys [email protected] New Users to Excel 13 August 3rd 07 01:08 AM
Getting handle to Row # dynamically prakash Excel Discussion (Misc queries) 1 September 12th 06 08:53 AM
Dynamically set a range? BKGT Excel Worksheet Functions 3 April 27th 06 03:26 PM
VBA Sort method w/more than 3 Keys KenRoy Excel Discussion (Misc queries) 1 August 26th 05 10:48 PM


All times are GMT +1. The time now is 09:32 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"