Stumped Excel 365 userform listbox is not showing all data

DThib

Active Member
Joined
Mar 19, 2010
Messages
464
Office Version
  1. 365
Platform
  1. Windows
Hello all,

I have a userform that populates a listbox with the data that matches another cells value. It runs and leaves option button for all results but not always the data from the cell it is recognizing Here is the code. I am thinking of putting in a delay to give the code time to trigger fully.
VBA Code:
Sub Operate_Click()

  Dim MCCw As Worksheet
  Dim MCCp As ListObject
  Dim MCCRP As ListColumn
  Dim AGo, WAdress As String
  Dim AGoAs, rng, ra, qcell As Range
  Dim ADate As Date
  Dim rowct As Integer
 
      Set MCCw = ThisWorkbook.Sheets("MCC Projects")
      Set MCCp = MCCw.ListObjects("MCC_Projects")
      Set MCCRP = MCCp.ListColumns("Responsible person")
      
   'On Error GoTo Line23
    If PM_Name <> "" Then
          'MsgBox "You Got It!", vbCritical, "2nd Level of Buttons"
         AGo = PM_Name.Value
       With MCCw.Range("C:C")
         Set rng = .Find(What:=AGo, After:=.Cells(1), LookIn:=xlValues, LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False)
            
           If Not rng Is Nothing Then
            wAddress = rng.Address
            rowct = 0
            For Each qcell In MCCw.Range("C:C")
             If AGo <> "" Then
              If qcell = AGo Then
               ProjectIDs_LB.AddItem
               ProjectIDs_LB.List(rowct, 0) = qcell(rng.Row, -1).Value   'Product
               rowct = rowct + 1
              
               Set rng = .FindNext(rng)
              End If
             End If
            Next qcell
            
           ElseIf rng Is Nothing Then
            MsgBox "Name not found" & vbCrLf & "Try Again"
           End If
       End With
    ElseIf PM_Name = "" Then
       MsgBox "Add a name", vbCritical + vbOKOnly, "Here we go..."
    End If

End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
I am not clear on your question. Do you mean that you add several items in your AddItem call, but then you don't see them when in the actual ListBox on the form? You have an If statement there--how do you know what you are actually loading?

I cannot figure out what this phrase is supposed to means "leaves option button for all results but not always the data from the cell it is recognizing" How does this involve option buttons?

Whatever your problem is, adding a delay won't fix it. You have no external events here that would cause a timing issue. Your code does not need extra time to "trigger fully."
 
Upvote 0
I have the listbox setup to have single select. I load it by identifying the common phrase for all examples and the result from a different column.
When I see the listbox result, I have option buttons with no data next to it and then some of them have the data next to them.
It is finding right amount of matches but not giving me the actual data in the cell it found.
Is that clearer?
The first If checks that you have entered a name (If PM_Name <> ""), the second is check that the name is found (If AGo <> "")
 
Upvote 0
How about:

VBA Code:
Sub Operate_Click()
  Dim r As Range, f As Range, cell As String
 
  If PM_Name.Value = "" Then Exit Sub
  Set r = ThisWorkbook.Sheets("MCC Projects").Range("C:C")
  Set f = r.Find(PM_Name.Value, , xlValues, xlWhole)
  If Not f Is Nothing Then
    cell = f.Address
    Do
      ProjectIDs_LB.AddItem f.Offset(, -1)
      Set f = r.FindNext(f)
    Loop While Not f Is Nothing And f.Address <> cell
  End If
End Sub

If it is not what you need, then explain with a data sample (use XL2BB tool mini sheet), what you have in the textbox, what data you have in the sheet and what data you expect in the listbox.
 
Upvote 0
Solution
gui_initial.png

There should be 3 entries that match.
Your code is clean, but nothing is triggered in the userform when I run it. It just sits there. The event "operate" is run through the Run Name button

My organization does not allow the tool to run but here is the arrangement.
Here are the 3 entries:
Project definitionProject TitleResponsible person
3-001696mfgagag;akg;lfkg;dakgDeborah Thibodeaux
3-001812galsjg;ljfg;ldj;gjdfghkjghhgkahgDeborah Thibodeaux
3-002389gkafdkg4595tuq0gauwgireajgjgDeborah Thibodeaux
 
Upvote 0
There are blank spaces on the right side of the name, correct those spaces and try again.
1643986754649.png


And change this:
Rich (BB code):
ProjectIDs_LB.AddItem f.Offset(, -2)
 
Upvote 0
Thank you!
Data is added by too many people, I think I will change the column of names to a dropdown list.
I was trying to adapt code I wrote for a far more complex pull and goofed up. I appreciate your help!
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,691
Members
449,117
Latest member
Aaagu

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