Once Listbox is populated can we then do a sort from there

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,983
Office Version
  1. 2007
Platform
  1. Windows
See screenshot for current Listbox in use.
The code in use populates the listbox by adding any HONDA related information from my sheet.

At present the list is small but will grow over time.
I was thinking & open to advice should i wish to look at information for say CB 600 F only can this be done from this list.
Meaning either hide all the other or what do you suggest.

Thanks


EaseUS_2025_02_26_10_19_47.jpg
 
As per the screenshot the pulled up values all relate to HONDA then a colour written in the sheets cells.
Is an option to have 3 separate buttons name as BLACK. CLEAR, & RED
Upon pressing one the current list would be cleared & only the values assigned to the colour choice shown

If i were to use the same code assuming thats they way to do it & lets talk about button RED
How would i alter the code & use to pull up only the RED values.

VBA Code:
  Dim r As Range, f As Range, Cell As String, added As Boolean
  Dim sh As Worksheet
  Dim i As Long
  Set sh = Sheets("MCLIST")
  sh.Select
  With ListBox1
    .Clear
    .ColumnCount = 4
    .ColumnWidths = "100;170;70;10"
    
    Set r = Range("C8", Range("C" & Rows.Count).End(xlUp))
    Set f = r.Find(TextBox1.Value, After:=r.Cells(r.Count), LookIn:=xlValues, LookAt:=xlPart)
    
    If Not f Is Nothing Then
      Cell = f.Address
      Do
         If Len(Cells(f.Row, "L").Value) <> 0 Then
          .AddItem f.Value
          .List(.ListCount - 1, 1) = f.Offset(, 1).Value ' MODEL
          .List(.ListCount - 1, 2) = f.Offset(, 6).Value ' YEAR
          .List(.ListCount - 1, 3) = f.Offset(, 9).Value ' CONNECTOR USED
          .List(.ListCount - 1, 4) = f.Row
         End If
        Set f = r.FindNext(f)
      Loop While f.Address <> Cell
      .TopIndex = 0
    End If
    End With
    
    TextBox2.Text = ActiveSheet.Range("L1").Value ' BLACK
    TextBox3.Text = ActiveSheet.Range("L2").Value ' CLEAR
    TextBox4.Text = ActiveSheet.Range("L3").Value ' GREY
    TextBox5.Text = ActiveSheet.Range("L4").Value ' RED
 
Last edited:
Upvote 0
Come up with this but i get variable not defined.
Relates to the word i entered being BLACK of which i need to find on my sheet

Code:
Private Sub ButtonBlack_Click()
ListBox1.Clear
  Dim r As Range, f As Range, Cell As String, added As Boolean
  Dim sh As Worksheet
  Dim i As Long
  Set sh = Sheets("MCLIST")
  sh.Select
  With ListBox1
    .Clear
    .ColumnCount = 4
    .ColumnWidths = "100;170;70;10"
    
    Set r = Range("C8", Range("C" & Rows.Count).End(xlUp))
    Set f = r.Find(BLACK.Value, After:=r.Cells(r.Count), LookIn:=xlValues, LookAt:=xlPart)
    
    If Not f Is Nothing Then
      Cell = f.Address
      Do
         If Len(Cells(f.Row, "L").Value) <> 0 Then
          .AddItem f.Value
          .List(.ListCount - 1, 1) = f.Offset(, 1).Value ' MODEL
          .List(.ListCount - 1, 2) = f.Offset(, 6).Value ' YEAR
          .List(.ListCount - 1, 3) = f.Offset(, 9).Value ' CONNECTOR USED
          .List(.ListCount - 1, 4) = f.Row
         End If
        Set f = r.FindNext(f)
      Loop While f.Address <> Cell
      .TopIndex = 0
    End If
    End With
    TextBox2.Text = ActiveSheet.Range("L1").Value ' BLACK
End Sub
 
Upvote 0
Your title says Once Listbox is populated can we then do....

So why not just click on a line with the color you want ?
VBA Code:
Private Sub ListBox1_Click()
    Dim i As Long
    Dim kolor As String
    
    kolor = ListBox1.List(ListBox1.ListIndex, 3)
    For i = ListBox1.ListCount - 1 To 0 Step -1
        If ListBox1.List(i, 3) <> kolor Then
            ListBox1.RemoveItem (i)
        End If
    Next i
    
End Sub
 
Upvote 0
Solution
I was trying to move on.

I will look at that once home.

Can you advise what the correct code should be for what I attempted so I then have 2 options to consider.
But I do like the sounds of your code.
 
Upvote 0

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