Edit existing working code to search for specific value & load listbox

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,884
Office Version
  1. 2007
Platform
  1. Windows
Hi,
The code shown is what i normally use where the user would type a value & the code would search & the load listbox with values.
Please can you edit the code for a known value to be searched without the need to type a value to search.

The value to search will allways be HONDA


VBA Code:
  Dim r As Range, f As Range, Cell As String, added As Boolean
  Dim sh As Worksheet
 
  Set sh = Sheets("MCLIST")
  sh.Select
  With ListBox1
    .Clear
    .ColumnCount = 3
    .ColumnWidths = "170;220;130"
    If MakerSearch.Value = "" Then Exit Sub
    Set r = Range("C8", Range("C" & Rows.Count).End(xlUp))
    Set f = r.Find(MakerSearch.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.Row
              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.Row
        End If
        Set f = r.FindNext(f)
      Loop While Not f Is Nothing And f.Address <> Cell
      MakerSearch = UCase(MakerSearch)
      .TopIndex = 0
      Else
      MsgBox "NO MAKER WAS FOUND USING THAT INFORMATION", vbCritical, "HONDA MOTORCYCLE MAKER SEARCH"
      MakerSearch.Value = ""
      MakerSearch.SetFocus
    End If
  End With
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Change this line:
VBA Code:
Set f = r.Find(MakerSearch.Value, LookIn:=xlValues, LookAt:=xlPart)

For this:
VBA Code:
Set f = r.Find("HONDA", , xlValues, xlPart, , , False)

And delete this line:
VBA Code:
If MakerSearch.Value = "" Then Exit Sub

complete:
Private Sub CommandButton1_Click()
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 = 3
.ColumnWidths = "170;220;130"
'If MakerSearch.Value = "" Then Exit Sub
Set r = Range("C8", Range("C" & Rows.Count).End(xlUp))
Set f = r.Find("HONDA", , xlValues, xlPart, , , False)
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.Row
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.Row
End If
Set f = r.FindNext(f)
Loop While f.Address <> Cell
'MakerSearch = UCase(MakerSearch)
.TopIndex = 0
Else
MsgBox "NO MAKER WAS FOUND USING THAT INFORMATION", vbCritical, "HONDA MOTORCYCLE MAKER SEARCH"
'MakerSearch.Value = ""
'MakerSearch.SetFocus
End If
End With
End Sub
VBA Code:
 
Last edited:
Upvote 0
Code complete:

VBA Code:
Private Sub CommandButton1_Click()
  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 = 3
    .ColumnWidths = "170;220;130"
    'If MakerSearch.Value = "" Then Exit Sub
    Set r = Range("C8", Range("C" & Rows.Count).End(xlUp))
    Set f = r.Find("HONDA", , xlValues, xlPart, , , False)
    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.Row
              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.Row
        End If
        Set f = r.FindNext(f)
      Loop While f.Address <> Cell
      'MakerSearch = UCase(MakerSearch)
      .TopIndex = 0
    Else
      MsgBox "NO MAKER WAS FOUND USING THAT INFORMATION", vbCritical, "HONDA MOTORCYCLE MAKER SEARCH"
      'MakerSearch.Value = ""
      'MakerSearch.SetFocus
    End If
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,226,063
Messages
6,188,653
Members
453,489
Latest member
jessrw

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