Search code examples
vbaexcelcomboboxtextbox

How do I use a combobox and textbox on a userform in VBA to search and find data on the active Excel spreadsheet?


I've been trying to look for answers for the past two days and I still don't understand how to code this. I am learning VBA as I go for a project but I am more familiar with vb.net coding. So for this project, I have to add/update/delete data. In the update and delete button clicks, I have to search for the data using the conditions that the information searched is under the column chosen from the combobox and it has data from what is keyed in the textbox.

I do not know how to code both conditions together. The farthest I have gotten based on research is coding a Find method that searches and selects through the active sheet cells only by what is keyed in the textbox. How would I code the combobox as one of the conditions linked with what is in the textbox in order to search the spreadsheet successfully?

This is my code so far:

Private Sub cmdSearch_Click()
    Dim strFindWhat As String
    strFindWhat = TextBox1.Text

    On Error GoTo ErrorMessage

    Cells.Find(What:=strFindWhat, After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _False).Select
    Exit Sub
    ErrorMessage:
        MsgBox ("The data you are searching for does not exist")

End Sub

Private Sub UserForm_Initialize()
    ComboBox1.List = Application.Transpose(Sheet1.Range("A1:D1").Value)
End Sub

Spreadsheet:

SpreadSheet

Delete button click:

Delete Button Click

Textbox used for Search:

Textbox used for Search

Need to implement the combobox condition to search text only within the column specified:

Need to implement the combobox condition to search text only within the column specified


Solution

  • So her is a new Solution. You have to declare 3 Public Variables in the UserForm1 Modul. So you can give them Values while the USerForm is open and Find the Naxt Values when you click multiple Times on the Search Button.

    'Public Variables 
        Public bolFirstSearch As Boolean
        Public rng As Excel.Range
        Public cellFound  As Excel.Range
    
    Private Sub ComboBox1_Change()
        bolFirstSearch = False
    End Sub
    
    Private Sub CommandButton1_Click()
    Dim strFindWhat As String
    Dim intRowCB As Integer
    
    On Error GoTo ErrorMessage
    If UserForm1.bolFirstSearch = False Then
    
        strFindWhat = TextBox1.Text
        intRowCB = Cells.Find(What:=ComboBox1.value, After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Column
        Set rng = Columns(intRowCB)
        rng.Select
            Set cellFound = rng.Find(What:=strFindWhat, After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
            cellFound.Select
    Else
    
        Set cellFound = rng.FindNext(cellFound)
        cellFound.Select
    End If
    UserForm1.bolFirstSearch = True
    Exit Sub
    ErrorMessage:
    
     MsgBox ("The data you are searching for does not exist")
    End Sub
    
    Private Sub UserForm_Initialize()
        ComboBox1.List = Application.Transpose(Sheet1.Range("A1:D1").Value)
        bolFirstSearch = False
    End Sub
    
    Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
        bolFirstSearch = False
    End Sub