Search worksheet for specific word then populate Listbox

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,226
Office Version
  1. 2007
Platform
  1. Windows
Morning,
I have a userform which when i select CheckBox1 i would like the code to search column G on my worksheet for the word YES then populate my ListBox with the required info.
I have the code below for which im using & follows my other codes on this form but im confused on how to set the word YES as the CheckBox1 value so the code knows what to search for when the user selecyts the CheckBox.

Thanks

Rich (BB code):
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
  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 = 3
    .ColumnWidths = "100;100;100"
    If CheckBox1.Value = "" Then Exit Sub
    Set r = Range("G3", Range("G" & Rows.Count).End(xlUp))
    Set f = r.Find(CheckBox1.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.Row
              added = True
              Exit For
          End Select
        Next
           If added = False Then
          .AddItem f.Value
          .List(.ListCount - 1, 1) = f.Row
          .List(i, 1) = f.Offset(, 1).Value
          .List(i, 2) = f.Offset(, 2).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
        End If
        Set f = r.FindNext(f)
      Loop While Not f Is Nothing And f.Address <> cell
      .TopIndex = 0
      Else
      MsgBox "NO CUSTOMER WAS FOUND", vbCritical, "CLONING INFORMATION MESSAGE"
      CheckBox1.Value = ""
    End If
  End With
End Sub
 
Last edited:

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Sorry for my mistake above BUT this is the code in use
Thanks

Rich (BB code):
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
  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 = 3
    .ColumnWidths = "100;100;100"
    If CheckBox1.Value = "" Then Exit Sub
    Set r = Range("G3", Range("G" & Rows.Count).End(xlUp))
    Set f = r.Find(CheckBox1.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(.ListCount - 1, 1) = f.Row
          .List(i, 1) = f.Offset(, 1).Value
          .List(i, 2) = f.Offset(, 2).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
        End If
        Set f = r.FindNext(f)
      Loop While Not f Is Nothing And f.Address <> cell
      .TopIndex = 0
      Else
      MsgBox "NO SNIFF DATA WAS FOUND", vbCritical, "CLONING INFORMATION MESSAGE"
      CheckBox1.Value = ""
    End If
  End With
  End If
End Sub
 
Upvote 0
Hi,
untested but see if this update to your code does what you want

VBA Code:
Private Sub CheckBox1_Click()
    Dim FoundCell       As Range
    Dim i               As Long
    Dim FirstAddress    As String, Search As String
    Dim sh              As Worksheet
  
    Search = "Yes"
  
    Set sh = ThisWorkbook.Worksheets("DETAILS")
  
    With Me.ListBox1
        .Clear
        .ColumnCount = 4
        .ColumnWidths = "100;100;100;0"
      
        If Not Me.CheckBox1.Value Then Exit Sub
      
        Set FoundCell = sh.Columns(7).Find(Search, LookIn:=xlValues, lookat:=xlPart)
        If Not FoundCell Is Nothing Then
            FirstAddress = FoundCell.Address
            Do
                .AddItem FoundCell.Value, i
                .List(i, 1) = FoundCell.Offset(, 1).Value
                .List(i, 2) = FoundCell.Offset(, 2).Value
              
                'record row (hidden)
                .List(i, 3) = FoundCell.Row
              
                i = i + 1
              
                Set FoundCell = sh.Columns(7).FindNext(FoundCell)
                If FoundCell Is Nothing Then Exit Do
              
            Loop While FoundCell.Address <> FirstAddress
          
        Else
            MsgBox "NO SNIFF DATA WAS FOUND", vbCritical, "CLONING INFORMATION MESSAGE"
            Me.CheckBox1.Value = False
        End If
    End With
End Sub

Dave
 
Upvote 0
Thanks for the reply.
I would like to keep the code similair to what i have for the other textboxes so i can then follow one code.
I managed to sort it by adding the word YES like so

Rich (BB code):
Set f = r.Find("YES", LookIn:=xlValues, lookat:=xlPart)
 
Upvote 0

Forum statistics

Threads
1,214,787
Messages
6,121,569
Members
449,038
Latest member
Guest1337

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