View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Wilbur[_3_] Wilbur[_3_] is offline
external usenet poster
 
Posts: 2
Default Dynamically set Sort keys

Hello All,

I'm trying to create a worksheet that has a database with the capability to
dynamically set each of the three sort keys. My database is in the range
from a5:g15, the field names are in row 5. I've created in cell drop down
lists using validation in cells a4, b4, c4. I'd like to use these dropdowns
to select sort keys 1-3 respectively.

Searching google I've found some posts that have something similar with a
single sort key that was posted by Steve Bell:

Dim x as Integer

x = Worksheetfunction.Match(Range("A3"),Range("A5:g15" ), 0)

Selection.Sort Key1:=Columns(x), Order1:=xlAscending, _
Header:=xlGuess,OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom

but I can't figure out how to modify it to accomodate three sort keys. I've
tried dimensioning two other variables y and z for the other sort keys and
the pasting the above code with x changed to y and z respectively....no
luck, I keep getting errors.

This is what I have so far.

Dim x as Integer
Dim y as Integer
Dim z as Integer

x = Worksheetfunction.Match(Range("A3"),Range("A5:g15" ), 0)
y = Worksheetfunction.Match(Range("b3"),Range("A5:g15" ), 0)
z = Worksheetfunction.Match(Range("c3"),Range("A5:g15" ), 0)

Selection.Sort Key1:=Columns(x), Order1:=xlAscending, _
Header:=xlGuess,OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom

Selection.Sort Key2:=Columns(y), Order1:=xlAscending, _
Header:=xlGuess,OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom

Selection.Sort Key3:=Columns(z), Order1:=xlAscending, _
Header:=xlGuess,OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom

Could anyone possibly help me figure out the proper syntax?...

Thanks for your help in advance.

Will