Userform 3 values met to show listbox results

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
6,151
Office Version
  1. 2024
Platform
  1. Windows
A few times we’ve been searching through our database to see if a job had been previously done but spending a lot of time doing so.
Thought maybe this would be the answer to our problems. What do you think.

A user form would have say 3 combo boxes & a listbox.

Each combobox would search a specific column, example first column A second column C & third column G

The user would make a selection from each combobox & press a command button.
If all 3 selected are found then show row in listbox.

Example.
Ford, Fiesta, Add new key.

If all exist show row number otherwise msg box saying not found etc.

Currently we’ve been looking using another search option for fiesta to then find it wasnt a Add new key job so looked again & again until it was.
 
A few times we’ve been searching through our database to see if a job had been previously done but spending a lot of time doing so.
Thought maybe this would be the answer to our problems. What do you think.

A user form would have say 3 combo boxes & a listbox.

Each combobox would search a specific column, example first column A second column C & third column G

The user would make a selection from each combobox & press a command button.
If all 3 selected are found then show row in listbox.

Example.
Ford, Fiesta, Add new key.

If all exist show row number otherwise msg box saying not found etc.

Currently we’ve been looking using another search option for fiesta to then find it wasnt a Add new key job so looked again & again until it was.

You should be more explicit in your requests.
How many columns does the list box have?
Which columns are you going to load into the list box?
Sheet name?
etc., etc., etc.

If you can't adapt the code to your needs, then you should explain in detail what you have and what you expect as a result.

Include images and sample minisheets; that would help me provide you with a suitable solution and not have you guessing all the time.

🧙‍♂️
 
Upvote 0
@DanteAmor
Morning so a few notes ive come across whilst trying to use the code above.

I have added this code to a button to clear the listbox & Combobox.
However i see a RTE9 Subscript out of range.
When i debug i see the following line in yellow

Rich (BB code):
Private Sub ResetForm_Click()
ListBox1.Clear      ** this clears listbox if i comment out the 3 combobox lines below ***
ComboBox1.Clear
ComboBox2.Clear
ComboBox3.Clear
End Sub


Rich (BB code):
Sub FilterData()
  Dim cmb1 As Variant, cmb2 As Variant, cmb3 As String
  Dim i As Long, j As Long, k As Long, n As Long
  Dim b As Variant
 
  ListBox1.Clear
  n = WorksheetFunction.CountIfs(Range("D:D"), ComboBox1.Value & "*", _
        Range("F:F"), ComboBox2.Value & "*", Range("G:G"), ComboBox3.Value & "*")
  ReDim b(1 To n, 1 To 7)
 
  For i = 1 To UBound(a, 1)
    If ComboBox1.Value = "" Then cmb1 = a(i, 4) Else cmb1 = ComboBox1.Value
    If ComboBox2.Value = "" Then cmb2 = a(i, 6) Else cmb2 = ComboBox2.Value
    If ComboBox3.Value = "" Then cmb3 = a(i, 7) Else cmb3 = ComboBox3.Value
       
    If a(i, 4) = cmb1 And a(i, 6) = cmb2 And a(i, 7) = cmb3 Then
      k = k + 1
      For j = 1 To UBound(a, 2)
        b(k, j) = a(i, j)      *** this line is shown when i debug the RTE9 ***
      Next
    End If
  Next
  ListBox1.List = b
End Sub
 
Upvote 0
Test the code before making any changes.

Don't delete the combos, because you'll delete all the items that were loaded.

If you want to clear the combo, use combobox1.value = ""

Test my code without making any changes.
 
Upvote 0
I have removed the code to clear combobox & listbox in order to test the code supplied.

A few things to mention / address if possible.

My worksheet values are added as jobs are completed & then column A which is the customers name is sorted A- Z for the user to find / locate customer easier.
Because of this when i click the drop down in the combobox the values are in no order from the sheet its been taken from, i understand why but im now looking up / down for a value to select.

The code i added to a command button was to clear the combobox & listbox once finished so the user could make another search.
Currently having to close the form then open again due to the RTE

Some results that enter the listbox are in need of an adjustment to column width.


This also gave me the same RTE and line in the code yelow wehn debugged
Rich (BB code):
combobox1.value = ""
 
Last edited:
Upvote 0
I can't help you any further.
You're not cooperating.
You're modifying the macro.
You're not providing the relevant information I requested.
You should start by providing sample data and images to explain what you need.
My code works with the following:

DANTE AMOR
ABCDEFG
1ABCDEFG
2A2B2C2A2E2C2G2
3A2B3C2A2E3C2G2
4A2B4C2A2E4C2G2
5A2B5C2A2E5C2G5
6A2B6C2A2E6C2G5
7A2B7C7A2E7C7G5
8A2B8C7A2E8C7G5
9A2B9C7A2E9C7G9
10A2B10C7A2E10C7G10
11A11B11C11A11E11C11G11
12A11B12C11A11E12C11G11
13A11B13C11A11E13C11G13
14A11B14C14A11E14C14G14
15A11B15C15A11E15C15G15
Hoja2


If you put my complete code in your userform with three combo boxes and a list box, you'll see that it works with the data above.
There's no need for a button; when you delete data in combos, the filter is done automatically.

😶
 
Upvote 0
I have used your code & items as mentioned.

I select from first combobox, the list is updated.
I then make a selection from second combobox, the list is updated.
I then make a selection from the third combobox, the list is updated.

Now i wish to make a new search hence why i wanted to clear the combobox but as you say use your code only i then make a selection from the first combobox & because selections are shown in combobox 2 & 3 i get the RTE like advis3ed.

This is why i mentioned about clearing the comboboxes without closing / opening the form
 
Upvote 0
Now i wish to make a new search hence why i wanted to clear the combobox but as you say use your code only i then make a selection from the first combobox & because selections are shown in combobox 2 & 3 i get the RTE like advis3ed.
Try:

VBA Code:
Dim a As Variant

Sub FilterData()
  Dim cmb1 As Variant, cmb2 As Variant, cmb3 As String
  Dim i As Long, j As Long, k As Long, n As Long
  Dim b As Variant
  
  ListBox1.Clear
  n = WorksheetFunction.CountIfs(Range("D:D"), ComboBox1.Value & "*", _
        Range("F:F"), ComboBox2.Value & "*", Range("G:G"), ComboBox3.Value & "*")
  If n = 0 Then
    MsgBox "There are no values to display"
    Exit Sub
  End If
  ReDim b(1 To n, 1 To 7)
  
  For i = 1 To UBound(a, 1)
    If ComboBox1.Value = "" Then cmb1 = a(i, 4) Else cmb1 = ComboBox1.Value
    If ComboBox2.Value = "" Then cmb2 = a(i, 6) Else cmb2 = ComboBox2.Value
    If ComboBox3.Value = "" Then cmb3 = a(i, 7) Else cmb3 = ComboBox3.Value
        
    If a(i, 4) = cmb1 And a(i, 6) = cmb2 And a(i, 7) = cmb3 Then
      k = k + 1
      For j = 1 To UBound(a, 2)
        b(k, j) = a(i, j)
      Next
    End If
  Next
  ListBox1.List = b
End Sub

Private Sub ComboBox1_Change()
  ComboBox2.Value = ""
  ComboBox3.Value = ""
  Call FilterData
End Sub
Private Sub ComboBox2_Change()
  ComboBox3.Value = ""
  Call FilterData
End Sub
Private Sub ComboBox3_Change()
  Call FilterData
End Sub

Private Sub UserForm_Activate()
  Dim dic1 As Object, dic2 As Object, dic3 As Object
  Dim i As Long
  Set dic1 = CreateObject("Scripting.Dictionary")
  Set dic2 = CreateObject("Scripting.Dictionary")
  Set dic3 = CreateObject("Scripting.Dictionary")
  
  a = Range("A1:G" & Range("D" & Rows.Count).End(3).Row).Value
  ListBox1.ColumnCount = 7
  
'first column D second column F & third column G
  For i = 2 To UBound(a, 1)
    dic1(Range("D" & i).Value) = Empty
    dic2(Range("F" & i).Value) = Empty
    dic3(Range("G" & i).Value) = Empty
  Next
  
  ComboBox1.List = dic1.keys
  ComboBox2.List = dic2.keys
  ComboBox3.List = dic3.keys
End Sub

😇
 
Upvote 0
Solution
This will be a job for tomorrow.

Do you have any advice on how to make column widths adjustable as some values are cut off at the end & not the full value is shown.

Also the order of values in combobox as currently there is no order & just random. This then is a task having to look up & down for the value you want to select
 
Upvote 0
Do you have any advice on how to make column widths adjustable as some values are cut off at the end & not the full value is shown.

Also the order of values in combobox as currently there is no order & just random. This then is a task having to look up & down for the value you want to select
These topics are not relevant to your original question.
Create a new thread.
 
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