Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Macro to Sort Automatically Based on Contents of Drop-down Box

Hello All,

I am trying to create a capability where a user of my spreadsheet would be
able to choose the primary sort key via a drop-down box (eg last name, first
name, etc) and then have the spreadsheet sort automatically. This drop down
box is located in cell b12. My table has headings(a13:p13) matching the
options in the drop down box. I have parts of the macro written, but I am
having trouble tying it all together. Basically, I would like the macro to
look at cell b12, match it with the appropriate heading and then sort
primarily based on that column of information. Below is an example of what I
have been working on (although it does not work)


Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)



If Target.Address = "$B$12" Then



If Range(B13).Value = Target.Value Then

Range("A14:P25").Select

Selection.Sort Key1:=Range("B14"), Order1:=xlAscending, Header:=xlGuess,
_

OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

End if



If Range(D13).Value = Target.Value Then

Range("A14:P25").Select

Selection.Sort Key1:=Range("D14"), Order1:=xlAscending, Header:=xlGuess,
_

OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

End if



If Range(E13).Value = Target.Value Then

Range("A14:P25").Select

Selection.Sort Key1:=Range("E14"), Order1:=xlAscending, Header:=xlGuess,
_

OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

End if



If Range(H13).Value = Target.Value Then

Range("A14:P25").Select

Selection.Sort Key1:=Range("H14"), Order1:=xlAscending, Header:=xlGuess,
_

OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

End if



If Range(I13).Value = Target.Value Then

Range("A14:P25").Select

Selection.Sort Key1:=Range("I14"), Order1:=xlAscending, Header:=xlGuess,
_

OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

End if



If Range(J13).Value = Target.Value Then

Range("A14:P25").Select

Selection.Sort Key1:=Range("J14"), Order1:=xlAscending, Header:=xlGuess,
_

OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

End if



End Sub

I would appreciate any suggestions on this little dilemma.
Thanks in advance,

A.


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 576
Default Macro to Sort Automatically Based on Contents of Drop-down Box

Ashleigh,

You can replace the sort key with Columns(x), instead of Range. Than you
don't need any of the If statements. You will need to error check for "no
selection" in B12.

Dim x as Integer

x = Worksheetfunction.Match(Range("B12"),Range("A13"P1 3"), 0)

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

--
sb
"Ashleigh K." wrote in message
...
Hello All,

I am trying to create a capability where a user of my spreadsheet would be
able to choose the primary sort key via a drop-down box (eg last name,

first
name, etc) and then have the spreadsheet sort automatically. This drop

down
box is located in cell b12. My table has headings(a13:p13) matching the
options in the drop down box. I have parts of the macro written, but I am
having trouble tying it all together. Basically, I would like the macro to
look at cell b12, match it with the appropriate heading and then sort
primarily based on that column of information. Below is an example of what

I
have been working on (although it does not work)


Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)



If Target.Address = "$B$12" Then



If Range(B13).Value = Target.Value Then

Range("A14:P25").Select

Selection.Sort Key1:=Range("B14"), Order1:=xlAscending,

Header:=xlGuess,
_

OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

End if



If Range(D13).Value = Target.Value Then

Range("A14:P25").Select

Selection.Sort Key1:=Range("D14"), Order1:=xlAscending,

Header:=xlGuess,
_

OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

End if



If Range(E13).Value = Target.Value Then

Range("A14:P25").Select

Selection.Sort Key1:=Range("E14"), Order1:=xlAscending,

Header:=xlGuess,
_

OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

End if



If Range(H13).Value = Target.Value Then

Range("A14:P25").Select

Selection.Sort Key1:=Range("H14"), Order1:=xlAscending,

Header:=xlGuess,
_

OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

End if



If Range(I13).Value = Target.Value Then

Range("A14:P25").Select

Selection.Sort Key1:=Range("I14"), Order1:=xlAscending,

Header:=xlGuess,
_

OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

End if



If Range(J13).Value = Target.Value Then

Range("A14:P25").Select

Selection.Sort Key1:=Range("J14"), Order1:=xlAscending,

Header:=xlGuess,
_

OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

End if



End Sub

I would appreciate any suggestions on this little dilemma.
Thanks in advance,

A.




Reply
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
How do I change contents of a cell based on drop down list selecte yateswdy Excel Worksheet Functions 2 October 21st 09 03:18 AM
macro with input msg based on cell contents Janelle S[_2_] Excel Discussion (Misc queries) 2 February 9th 08 11:47 PM
Launch macro when drop-down contents change... Tom Mackay Excel Discussion (Misc queries) 1 September 6th 07 08:52 PM
Macro to clear contents of unprotected cells AND drop down boxes JB2010 Excel Discussion (Misc queries) 3 March 30th 06 10:13 AM
Macro to Sort automatically when file/save is selected KDG Excel Discussion (Misc queries) 3 December 20th 05 08:28 PM


All times are GMT +1. The time now is 10:20 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"