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"
 

Some videos you may like

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop

JAbraha1

New Member
Joined
Aug 2, 2009
Messages
15
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
 

JAbraha1

New Member
Joined
Aug 2, 2009
Messages
15
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
 

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Threads
1,106,585
Messages
5,512,231
Members
408,885
Latest member
binduchekuri

This Week's Hot Topics

  • Sort code advice please
    Hi, I have the code below which im trying to edit but getting a little stuck. This was the original code which worked fine,columns A-F would sort...
  • SUMPRODUCT with nested If statement
    Hi everyone, Hope you're all well. I'm hoping someone will be able to point me in the right direction with a problem I'm having with a SUMPRODUCT...
  • VBA - simple sort is killing me!
    Hello all! This should be so easy, but not for me, apparently! I have a table of data that can be of varying lengths and widths. My current macro...
  • Compare Two Lists
    I have two Lists and I need to be able to Identify differences between them. List 100 comes from a workbook - the other is downloaded form the...
  • Formula that deducts points for each code I input.
    I am trying to create a formula that will have each student in my class start at 100 points and then for each code that I enter (PP for Poor...
  • Conditional formatting formula required for day of week and a value
    Hi, I have a really simple spreadsheet where column A is the date, column B is the activity total shown as a number and column C states the day of...
Top