Listbox results position

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,226
Office Version
  1. 2007
Platform
  1. Windows
Evening,
I have a userform with 3 textboxes & a listbox.
In the listbox are 5 columns

In the textbox named Customer if i type ZAC the results appear like this,
ZAC, HONDA, CBR, 2011, YES

So now in textbox named Make i type HONDA the results appear like this
HONDA, ZAC, CBR. 2011, YES

My goal is to have the listbox put the results in the same order each time as opposed having the fisrt column being what i typed in the search box.
Below is the code for the textbox named Make BUT please can you advise which part makes the searched value get placed in the first column.

Did i explain correctly for you ?
Thanks

Rich (BB code):
Private Sub TextBoxMAKE_Change()
TextBoxMAKE = UCase(TextBoxMAKE)
  Dim R As Range, f As Range, cell As String, added As Boolean
  Dim sh As Worksheet
  
  Set sh = Sheets("DETAILS")
  sh.Select
  With ListBox1
    .Clear
    .ColumnCount = 5
    .ColumnWidths = "150;180;150;100;80"
    If TextBoxMAKE.Value = "" Then Exit Sub
    Set R = Range("B3", Range("B" & Rows.Count).End(xlUp))
    Set f = R.Find(TextBoxMAKE.Value, LookIn:=xlValues, LookAt:=xlPart)
    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(, -1).Value
              .List(i, 2) = f.Offset(, 1).Value
              .List(i, 3) = f.Offset(, 2).Value
              .List(i, 4) = f.Offset(, 5).Value
              added = True
              Exit For
          End Select
        Next
        If added = False Then
          .AddItem f.Value
          .List(.ListCount - 1, 1) = f.Offset(, -1).Value
          .List(.ListCount - 1, 2) = f.Offset(, 1).Value
          .List(.ListCount - 1, 3) = f.Offset(, 2).Value
          .List(.ListCount - 1, 4) = f.Offset(, 5).Value
        End If
        Set f = R.FindNext(f)
      Loop While Not f Is Nothing And f.Address <> cell
      TextBoxSearch = UCase(TextBoxSearch)
      .TopIndex = 0
      Else
      MsgBox "NO ITEM WAS FOUND USING THAT INFORMATION", vbCritical, "DATABASE SHEET ITEM SEARCH"
      TextBoxMAKE.Value = ""
      TextBoxMAKE.SetFocus
    End If
  End With
End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
In the code, you will see these two lines that set the first two items
VBA Code:
 .List(i, 1) = f.Offset(, -1).Value
.List(i, 2) = f.Offset(, 1).Value

The only difference between these lines is the number near the end (1 and -1)
If the code for the other textbox has the 1 and -1 swapped round, change it so these two lines are the same
 
Upvote 0
Hi,
That didnt amke any difference so i have below the code for it

Rich (BB code):
Private Sub TextBoxCUSTOMER_Change()
  TextBoxCUSTOMER = UCase(TextBoxCUSTOMER)
  Dim R As Range, f As Range, cell As String, added As Boolean
  Dim sh As Worksheet
  
  Set sh = Sheets("DETAILS")
  sh.Select
  With ListBox1
    .Clear
    .ColumnCount = 6
    .ColumnWidths = "180;150;150;100;80;80"
    If TextBoxCUSTOMER.Value = "" Then Exit Sub
    Set R = Range("A3", Range("A" & Rows.Count).End(xlUp))
    Set f = R.Find(TextBoxCUSTOMER.Value, LookIn:=xlValues, LookAt:=xlPart)
    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(, 1).Value
              .List(i, 2) = f.Offset(, 2).Value
              .List(i, 3) = f.Offset(, 3).Value
              .List(i, 4) = f.Offset(, 6).Value
              .List(i, 5) = f.Offset(, 7).Value
              added = True
              Exit For
          End Select
        Next
        If added = False Then
          .AddItem f.Value
          .List(.ListCount - 1, 1) = f.Offset(, 1).Value
          .List(.ListCount - 1, 2) = f.Offset(, 2).Value
          .List(.ListCount - 1, 3) = f.Offset(, 3).Value
          .List(.ListCount - 1, 4) = f.Offset(, 6).Value
          .List(.ListCount - 1, 5) = f.Offset(, 7).Value
        End If
        Set f = R.FindNext(f)
      Loop While Not f Is Nothing And f.Address <> cell
      TextBoxSearch = UCase(TextBoxSearch)
      .TopIndex = 0
      Else
      MsgBox "NO CUSTOMER WAS FOUND", vbCritical, "CLONING INFORMATION MESSAGE"
      TextBoxCUSTOMER.Value = ""
      TextBoxCUSTOMER.SetFocus
    End If
  End With
End Sub
 
Upvote 0
It's difficult to see what's going on from that, I think there must be more code involved
 
Upvote 0
This is the only code but i dont think its what you advised.
Ive tried many of the figures to see what happens & just cant swap them around so i think possibly some code needs to be added as opposed to editing what is currently in use
 
Upvote 0

Forum statistics

Threads
1,214,922
Messages
6,122,281
Members
449,075
Latest member
staticfluids

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