Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 237
Default Sort command

Hey guys,

I have a name in each cell from A6 through U6. The
objective is to be able to sort by each of these names. I
will use a form that will contain 3 objects: A DROP DOWN
BOX, OK button, and CANCEL button. I need for each name
in range A6:U6 to show up in the drop down box. I will
select a name from the drop down box and click the OK
button. When I click the OK button, I need it to sort
data in range A7:U55 by the name I selected from the drop
down box.

Can anyone tell me the code?

Thanx


Todd Huttenstine
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Sort command

code is untested and may contain typos:

Private Sub cmdOK_click() '< == OK
Dim rng As Range, rng1 As Range
Dim res As Variant
Dim rng2 As Range
With Worksheets("sheet1")
Set rng = .Range("A6:U55")
Set rng1 = .Range("A6:U6")
End With
res = Application.Match(combobox1.Value, rng1, 0)
If Not IsError(res) Then
Set rng2 = rng1(1, res)
rng.Sort Key1:=rng2, Order1:=xlAscending, _
Header:=xlYes, OrderCustom:=1, _
MatchCase:=False, Orientation:=xlTopToBottom
End if
Unload Me
End Sub


Private Sub Userform_Initialize
Dim rng As Range
With Worksheets("sheet1")
Set rng = .Range("A6:U6")
End With
for each cell in rng
Combobox1.AddItem cell.Value
Next
End With

Private Sub cmdCancel_Click()
Unload Me
End Sub

--
Regards,
Tom Ogilvy



Todd Huttenstine wrote in message
...
Hey guys,

I have a name in each cell from A6 through U6. The
objective is to be able to sort by each of these names. I
will use a form that will contain 3 objects: A DROP DOWN
BOX, OK button, and CANCEL button. I need for each name
in range A6:U6 to show up in the drop down box. I will
select a name from the drop down box and click the OK
button. When I click the OK button, I need it to sort
data in range A7:U55 by the name I selected from the drop
down box.

Can anyone tell me the code?

Thanx


Todd Huttenstine



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 48
Default Sort command

much smaller and quicker than mine. I'll use it from now on.

"Tom Ogilvy" wrote in message
...
code is untested and may contain typos:

Private Sub cmdOK_click() '< == OK
Dim rng As Range, rng1 As Range
Dim res As Variant
Dim rng2 As Range
With Worksheets("sheet1")
Set rng = .Range("A6:U55")
Set rng1 = .Range("A6:U6")
End With
res = Application.Match(combobox1.Value, rng1, 0)
If Not IsError(res) Then
Set rng2 = rng1(1, res)
rng.Sort Key1:=rng2, Order1:=xlAscending, _
Header:=xlYes, OrderCustom:=1, _
MatchCase:=False, Orientation:=xlTopToBottom
End if
Unload Me
End Sub


Private Sub Userform_Initialize
Dim rng As Range
With Worksheets("sheet1")
Set rng = .Range("A6:U6")
End With
for each cell in rng
Combobox1.AddItem cell.Value
Next
End With

Private Sub cmdCancel_Click()
Unload Me
End Sub

--
Regards,
Tom Ogilvy



Todd Huttenstine wrote in message
...
Hey guys,

I have a name in each cell from A6 through U6. The
objective is to be able to sort by each of these names. I
will use a form that will contain 3 objects: A DROP DOWN
BOX, OK button, and CANCEL button. I need for each name
in range A6:U6 to show up in the drop down box. I will
select a name from the drop down box and click the OK
button. When I click the OK button, I need it to sort
data in range A7:U55 by the name I selected from the drop
down box.

Can anyone tell me the code?

Thanx


Todd Huttenstine





  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 48
Default Sort command

Add this code to your userform initialize event complete it for your
purpose:
ComboBox1.AddItem ""
ComboBox1.AddItem Cells(6, 1) ' equals A6
ComboBox1.AddItem Cells(6, 2) ' equals B6
ComboBox1.AddItem Cells(6, 3) 'C6
ComboBox1.AddItem Cells(6, 4) 'D6
ComboBox1.AddItem Cells(6, 5) 'E6
ComboBox1.AddItem Cells(6, 6) 'F6
this will fill the contents of the cells into your combobox or dropdown.
Now add this code to your "Okay" command button on your userform. Again
complete as you need. You might want to play around with this code and try
using Select Case instead of all my IF's, but user preference.
'Checks for which cell combobox is equal to
If ComboBox1.Value = Cells(6,1) Then
Range("A7:U55").Select 'selects the range to sort
Selection.Sort Key1:=Range("A6"), ' sets the order to sort
Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
ElseIf ComboBox1.Value = Cells(6,2) Then
Range("A7:U55").Select
Selection.Sort Key1:=Range("B6"), Order1:=xlAscending, Header:=xlGuess,
_
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
ElseIf ComboBox1.Value = Cells(6,3) Then
Range("A7:U55").Select
Selection.Sort Key1:=Range("C6"), Order1:=xlAscending,
Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

End If
This should get you started.
HTH
"Todd Huttenstine" wrote in message
...
Hey guys,

I have a name in each cell from A6 through U6. The
objective is to be able to sort by each of these names. I
will use a form that will contain 3 objects: A DROP DOWN
BOX, OK button, and CANCEL button. I need for each name
in range A6:U6 to show up in the drop down box. I will
select a name from the drop down box and click the OK
button. When I click the OK button, I need it to sort
data in range A7:U55 by the name I selected from the drop
down box.

Can anyone tell me the code?

Thanx


Todd Huttenstine



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
Pivot Table Error Message - "Command Text not set for command obje Jeff Divian Excel Discussion (Misc queries) 0 November 7th 07 10:26 PM
Why is the sort command in excel 2003 shaded out? jico New Users to Excel 7 September 28th 06 06:05 PM
Sort command does not sort some columns? BillyBob New Users to Excel 4 May 11th 06 04:16 PM
how do i trace back my sort command on a column ????? rp Charts and Charting in Excel 1 December 1st 05 01:09 PM
sort command Lady Layla New Users to Excel 8 March 31st 05 04:54 PM


All times are GMT +1. The time now is 04:00 AM.

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"