Yodelayheewho
New Member
- Joined
- Jun 23, 2017
- Messages
- 13
My userform has a multicolumn listbox with 10 columns. I created command buttons as my headers.
I searched for a way to sort each column by clicking its respective command button header. I also wanted the command button to 'toggle' so each consecutive click would toggle between ascending and descending order. I found the following thread that got me pretty far: How do I sort My Listbox in a Userform?
I entered this code for the first column. I works, the column sorts a to z, then z to a, etc. However, the rest of the column data doesn't sort with it. I'm very new to VBA, so my code has notes. Here is the code from the old thread with my controls and notes...
The following image shows the results after I click on the 'Shop Order' header (command button). Notice the entire row does not sort as it should.
I would really appreciate the assistance.
I searched for a way to sort each column by clicking its respective command button header. I also wanted the command button to 'toggle' so each consecutive click would toggle between ascending and descending order. I found the following thread that got me pretty far: How do I sort My Listbox in a Userform?
I entered this code for the first column. I works, the column sorts a to z, then z to a, etc. However, the rest of the column data doesn't sort with it. I'm very new to VBA, so my code has notes. Here is the code from the old thread with my controls and notes...
VBA Code:
[/COLOR]
Option Explicit
[COLOR=rgb(97, 189, 109)]'Sorting code that follows, must be placed at the top, above, and outside of all remaining procedures***[/COLOR]
Public x&
Public y&
Public z As String
Public bln As Boolean [COLOR=rgb(65, 168, 95)]‘True or false values[/COLOR]
Private Sub [COLOR=rgb(147, 101, 184)]cmbShopOrdNum1[/COLOR]_Click()
bln = Not bln
Select Case bln
Case 1
[COLOR=rgb(97, 189, 109)]'Sort ascending[/COLOR]
With[COLOR=rgb(147, 101, 184)] lstMaster[/COLOR]
For x = 0 To .ListCount - 2
For y = x + 1 To .ListCount - 1
If .List(x) > .List(y) Then
z = .List(y)
.List(y) = .List(x)
.List(x) = z
End If
Next y
Next x
End With
Case 0[COLOR=rgb(97, 189, 109)]
'Sort descending[/COLOR]
With [COLOR=rgb(147, 101, 184)]lstMaster[/COLOR]
For x = 0 To .ListCount - 2
For y = x + 1 To .ListCount - 1
If .List(x) < .List(y) Then
z = .List(y)
.List(y) = .List(x)
.List(x) = z
End If
Next y
Next x
End With
End Select
End Sub
[COLOR=rgb(44, 130, 201)]
The following image shows the results after I click on the 'Shop Order' header (command button). Notice the entire row does not sort as it should.
I would really appreciate the assistance.