Forum Discussion
VBA Search for Multiple Criteria
If I understand correctly, then I think you will need to add an AND operator to your if statement to evaluate both conditions.
But, if you want to load multiple results into separate sets of text boxes, then I think you will need a counter variable to keep a running total of number of matches and then, based on that running total, load your next match in the next set of text boxes set up to hold the results (the Select Case statement in the below example is set up for three).
However, if you think you might expand the number of matches in the future, you could use a list box with two columns to hold the matches. And, if you wanted to, you could add functions to update only matches you select from the listbox or all matches at once. I attached a screen shot as an example.
Private Sub CommandButton1_Click()
Dim Current_Status As String
dim count as long
Current_Status=Trim(TextBox1.Text)
lastrow=Worksheets("Overall").Cells(Rows.Count, 1).End(x1Up).Row
For i =2 To lastrow
With Worksheets("Overall")
If .Cells(i, 1).Value=Current_Status And .Cells(i, 2).Value=CRITERIA2 Then
count = count+1
Select Case count
Case 1
TextBox2.Text = .Cells(i, 3).Value
TextBox14.Text = .Cells(i, 11).Value
Case 2
' Second set of textboxes here.
Case 3
' Third Set of textboxes here, etc.
End Select
End If
End With
Next
End Sub
Sorry for the delay in responding. Things are hectic. Getting close to finishing this project using your examples and code. Thanks for your assistance.
I have recreated the Userform minus the graphics on the Search and Update buttons.
For the coding just need some clarification: Criteria 1 = TextBox1 (from column 6 of Overall worksheet). Therefore, in TextBox1 the criteria will be Completed / Yes or Completed / No or can I just type Completed with a wildcard in the code to recognize any text after Completed? For Criteria 2 = Textbox2 (from column 5 of Overall worksheet) the criteria will be a vendor like ABC, XYZ, etc.
Click Search and the Results will populate in the List Box?
I only need one TextBox in Update. So TextBox3 is the date (for column 11 of Overall worksheet) that will close the orders found from the criteria. Adjusted the UserForm for this.
When I run DeBug, getting error - End If without block if
Again, thank you for your assistance.
- JMB17Aug 11, 2020Bronze Contributor
You could add a wildcard option for Criteria 1. One way would be to add a checkbox to enable wildcards, which controls a module variable. Then, the search function could check that module variable value and either match the text exactly or compare the text using the Like operator.
https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/like-operator
I made the change to the example workbook and included comments where I made changes.
The error you noted appears to be an "If/Then" statement that is missing an "End If." You'll need to check your If/Then blocks and make sure there is a matching End If. I'd have to see the code to be more helpful than that.
- RoyJrAug 13, 2020Copper Contributor
OK. Still having issues even after using the code you provided above.
Attached is an example of my excel file. Here is the breakdown:
From Userform2 on Dashboard, I search for two criteria in TextBox 1 (from Overall column C or 3) and Textbox2 (from Overall column D or 4). Once found and populated in ListBox, I enter a Date in Userform2 Textbox3. Once the Update is clicked, the data will populate in Overall column E or 5.
>>>Just for info: The Overall Column C will change (from a formula on this worksheet) so this data will not be found again.
If there is a better please option, please let me know.
Thank you,
Roy
- JMB17Aug 14, 2020Bronze ContributorIs it possible to include the user form in the workbook? I'm afraid I won't be able to do much without seeing the code.