Reputation: 1
I have a problem in my VBA form in Excel and I'm trying to filter the value of a combobox using VLOOKUP
from whatever I type in the textbox. How do I achieve this?
My code is:
Private Sub btnTemplateSearch_Click()
Dim filterInfo As Range
Set filterInfo = Worksheets("InfoDump").Range("E2:F46")
txtTemplateFilter.Text = filterInfo.Columns(2).Value
Me.cboTemplateType.List = Application.WorksheetFunction.VLookup(Me.txtTemplateFilter.Text, filterInfo,2,True)
Upvotes: 0
Views: 988
Reputation: 5770
Below is an example of a block of code that can be used to filter the list of ComboBox
entries. I decided to Dim
an array variable ListForComboBox
at the module level such that all procedures in the module can access it. It gets populated at the form's init event by calling LoadListForComboboxArray
. You can make changes to this procedure to update the range of input values or any other changes. The combobox's list property is set to this array.
Here's the caveat: you may want to consider tuning the below for performance. I included a textbox, and in the change event, I make a call to set the global array to a filtered version of the original array, based on textbox value. That means that the code will filter the array everytime you type a letter (so if you type "ABC", it will filter three times, creating a new array each time). You may want to assign that function to a different event (textbox exit, maybe) such that the code only fires once you leave the text box, and only once.
Let me know if you have trouble adapting the code:
Dim ListForCombobox() As String
Private Sub TextBox1_Change()
Me.ComboBox1.List = Filter(ListForCombobox, Me.TextBox1.Value)
Debug.Print "CALLED"
End Sub
Private Sub UserForm_Initialize()
LoadListForComboboxArray
Me.ComboBox1.List = ListForCombobox
End Sub
Private Sub LoadListForComboboxArray()
Dim rng As Range
Set rng = Sheets("Sheet1").Range("A1:A11")
ReDim ListForCombobox(1 To rng.Rows.Count)
For i = 1 To rng.Rows.Count
ListForCombobox(i) = rng(i).Value
Next i
Debug.Print ListForCombobox(1)
End Sub
Upvotes: 1