Custom Sort Form

JAbraha1

New Member
Joined
Aug 2, 2009
Messages
15
I have a form, that allows me to sort 3 fields, asc/desc (just like if you used the custom sort form). But I don't know how to change my code, to give them the option of sorting horizontally. (like if you selected options, then rows)

Here is my code under my current sort form. again, three fields, asc/desc:

Dim n As Long
Dim sortItem(1 To 3) As String
Dim sortOrder(1 To 3) As Long
Dim r, d(1 To 3) As Range
Dim Temp, sortString As String

sortItem(1) = cbSort1.Value
sortItem(2) = cbSort2.Value
sortItem(3) = cbSort3.Value
If cbSortOrder1.Value = "Descending" Then
sortOrder(1) = xlDescending
Else
sortOrder(1) = xlAscending
End If
If cbSortOrder2.Value = "Descending" Then
sortOrder(2) = xlDescending
Else
sortOrder(2) = xlAscending
End If
If cbSortOrder3.Value = "Descending" Then
sortOrder(3) = xlDescending
Else
sortOrder(3) = xlAscending
End If
For n = 1 To 3
Temp = Trim(Replace(Left(sortItem(n), 2), ".", "")) & "15"
If Temp <> "15" Then
Set d(n) = Range(Temp, Temp)
End If
Next n


If Not d(1) Is Nothing Then
If d(2) Is Nothing Then
Set d(3) = Nothing
End If
Set r = Range("sort")
If Not d(3) Is Nothing Then
r.Sort Key1:=d(1), Order1:=sortOrder(1), Key2:=d(2), Order2:=sortOrder(2), Key3:=d(3), Order3:=sortOrder(3), Orientation:=xlSortColumns
Else
If Not d(2) Is Nothing Then
r.Sort Key1:=d(1), Order1:=sortOrder(1), Key2:=d(2), Order2:=sortOrder(2), Orientation:=xlSortColumns
Else
r.Sort Key1:=d(1), Order1:=sortOrder(1), Orientation:=xlSortColumns
End If
End If
End If



In case you need it, here's the remaining code for the current columns you can choose from to sort. (columns B:E) I need rows (10:12)

Dim n As Long

frmCustomSort.cbSort1.Clear
frmCustomSort.cbSort2.Clear
frmCustomSort.cbSort3.Clear

frmCustomSort.cbSort1.AddItem "B. Cluster"
frmCustomSort.cbSort1.AddItem "C. Store Number"
frmCustomSort.cbSort1.AddItem "D. Store Name"
frmCustomSort.cbSort1.AddItem "E. Vendor Count"

For n = 0 To frmCustomSort.cbSort1.ListCount - 1
frmCustomSort.cbSort2.AddItem frmCustomSort.cbSort1.List(n)
frmCustomSort.cbSort3.AddItem frmCustomSort.cbSort1.List(n)
Next n
frmCustomSort.cbSortOrder1.Clear
frmCustomSort.cbSortOrder2.Clear
frmCustomSort.cbSortOrder3.Clear

frmCustomSort.cbSortOrder1.AddItem "Ascending"
frmCustomSort.cbSortOrder2.AddItem "Ascending"
frmCustomSort.cbSortOrder3.AddItem "Ascending"
frmCustomSort.cbSortOrder1.AddItem "Descending"
frmCustomSort.cbSortOrder2.AddItem "Descending"
frmCustomSort.cbSortOrder3.AddItem "Descending"
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
I should elaborate a little...

I know to change the orientation to Orientation:=xlSortRows, but I'm getting the debug error when it gets to:

For n = 1 To 3
Temp = Trim(Replace(Left(sortItem(n), 2), ".", "")) & "15"
If Temp <> "15" Then
Set d(n) = Range(Temp, Temp)

I have no idea what this means
 
Upvote 0
I figured it out!

Sorry! :LOL:

I changed the :

For n = 1 To 3
Temp = Trim(Replace(Left(sortItem(n), 2), ".", "")) & "15"
If Temp <> "15" Then
Set d(n) = Range(Temp, Temp)

Which was used for sorting top to bottom, to:

For n = 1 To 3
Temp = "F" & Trim(Replace(Left(sortItem(n), 3), ".", ""))
If Temp <> "F" Then
Set d(n) = Range(Temp, Temp)
End If
Next n

I had to change the range from A15, B15, etc.... to F10, F11, F12
 
Upvote 0

Forum statistics

Threads
1,215,054
Messages
6,122,895
Members
449,097
Latest member
dbomb1414

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top