Sorting listbox A-Z when userform is populated

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,832
Office Version
  1. 2007
Platform
  1. Windows
Morning,
I am using the following code & have also supplied screen shot of userform.

When the listbox is populated i see customers name top to bottom in the sort of Z to A
My goal is to sort top to bottom A to Z

Please advise what i need to do.
Thanks


Rich (BB code):
Private Sub SearchForm_Click()
  Dim R As Range, f As Range, cell As String, added As Boolean
  Dim sh As Worksheet
  
  Set sh = Sheets("DATABASE")
  sh.Select
  With ListBox1
    .Clear
    .ColumnCount = 5
    .ColumnWidths = "150;210;190;190;50"
    If ComboBox1.Value = "" Then Exit Sub
    Set R = Range("I6", Range("I" & Rows.Count).End(xlUp))
    Set f = R.Find(ComboBox1.Value, LookIn:=xlValues, LookAt:=xlWhole)
    If Not f Is Nothing Then
      cell = f.Address
      Do
        added = False
        For i = 0 To .ListCount - 1
          Select Case StrComp(.List(i), f.Value, vbTextCompare)
            Case 0, 1
              .AddItem f.Value, i
              .List(i, 1) = f.Offset(, -8).Text
              .List(i, 2) = f.Offset(, -5).Value
              .List(i, 3) = f.Offset(, -3).Value
              .List(i, 4) = f.Offset(, -2).Value
              .List(i, 5) = f.Offset(, -1).Value
              
              added = True
              Exit For
          End Select
        Next
        If added = False Then
              .AddItem f.Value
              .List(.ListCount - 1, 1) = f.Offset(, -8).Text
              .List(.ListCount - 1, 2) = f.Offset(, -5).Value
              .List(.ListCount - 1, 3) = f.Offset(, -3).Value
              .List(.ListCount - 1, 4) = f.Offset(, -2).Value
              .List(.ListCount - 1, 5) = f.Offset(, -1).Value
              
        End If
        Set f = R.FindNext(f)
      Loop While Not f Is Nothing And f.Address <> cell
      .TopIndex = 0
    Else
      
    End If
  End With
  
End Sub
 

Attachments

  • EaseUS_2023_07_ 3_09_55_44.jpg
    EaseUS_2023_07_ 3_09_55_44.jpg
    27.1 KB · Views: 21

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
I am using the following code
I'm sorry to disappoint you, but the code you posted doesn't order the data, it just puts the data in the listbox just as you have it on the sheet.

If you have the data in the sheet sorted from Z-A then simply order the data in the sheet from A-Z by customer name.

If you don't want to sort the sheet and want to do it in code.

So change this line:
VBA Code:
Select Case StrComp(.List(i), f.Value, vbTextCompare)

By this line:
VBA Code:
Select Case StrComp(.List(i, 1), sh.Range("A" & f.Row).Value, vbTextCompare)


:cool:
 
Upvote 0
Hi,
That didnt work for me.
I supply screen shot of a search with more values & info shown.

I search column I for AUTEL IM 508
Once the code finds a match it then takes the values from columns A D F G on the worksheet
As you can see in the screen shot the values are entered as requested BUT the issue im trying to explain is the way column A values are entered.
Its ordered from Z down the page to A where i would like it reversed to A down the page to Z
 

Attachments

  • EaseUS_2023_07_ 4_05_18_50.jpg
    EaseUS_2023_07_ 4_05_18_50.jpg
    134.6 KB · Views: 23
Upvote 0
Just a thought.
What if before the values are entered in the Listbox they are placed on the worksheet, then sorted from A-Z then entered in the Listbox ?
Would that work if so please advise

The search for value is always column I
The sortn of A to Z will always be column A

Thanks
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

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