VBA Userform search and place results into a ListBox

Excelstudent33

New Member
Joined
Dec 9, 2016
Messages
4
Please forgive me if my question isn't fully clear or if I miss a detail, it's my fist time posting a question here so go easy on me.

I'm working on a Userform that allows you to select search a client database. You should be able to select one criteria that relates to the column headers or to select all columns.

My problem is that every time I run my code it never finds anything and always gives me my error, that I set, instead. I'm still really new to VBA so I'm having a hard time figuring out what's wrong and was hoping you guys might be able to help.

The code refers to variables set in other macros
The variables that are set are as follows.

cmdHeader - ComboBox with list of header/column criteria to choose from
txtSearch - TextBox where the user can input the exact word to search for.
"AH3" - Criteria Cell where an advanced filter that copies to a new location. Refers to txtSearch
"AH2" - Cell where the advanced filter displays what column it found the data in
lstClient - ListBox where the rows that contain matched data should be displayed

Any help would be greatly appreciated. Thanks in advance!

Code:
Private Sub cmdGetClient_Click()
[COLOR=#008000]'dim the variables[/COLOR]
    Dim Crit As Range
    Dim FindMe As Range
    Dim DataSH As Worksheet
[COLOR=#008000]    'Error handler[/COLOR]
    On Error GoTo errHandler:
[COLOR=#008000]    'Set object variables[/COLOR]
    Set DataSH = Sheet4
[COLOR=#008000]    'Hold in memory and stop screen flicker[/COLOR]
    Application.ScreenUpdating = False
[COLOR=#008000]    '/////////////////////////////////////////////////[/COLOR]
[COLOR=#008000]    'If header is selected add the criteria[/COLOR]
    If Me.cmdHeader.Value <> "All Columns" Then
        If Me.txtSearch = "" Then
            DataSH.Range("AH3") = ""
        Else
            DataSH.Range("AH3") = "*" & Me.txtSearch.Value & "*"
        End If
    End If
[COLOR=#008000]    '/////////////////////////////////////////////////[/COLOR]
[COLOR=#008000]    'If "All Columns" is selected[/COLOR]
    If Me.cmdHeader.Value = "All Columns" Then
[COLOR=#008000]        'Find the value in the column[/COLOR]
        Set FindMe = DataSH.Range("B4:S10000").Find(What:=txtSearch, LookIn:=xlValues, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)
[COLOR=#008000]        'Variable for criteria header[/COLOR]
        Set Crit = DataSH.Cells(3, FindMe.Column)
[COLOR=#008000]        'If no criteria is added to the search[/COLOR]
        If Me.txtSearch = "" Then
            DataSH.Range("AH3") = ""
            DataSH.Range("AH2") = ""
        Else
[COLOR=#008000]            'Add values from the search[/COLOR]
            DataSH.Range("AH2") = Crit
            If Crit = "ID" Then
                DataSH.Range("AH3") = Me.txtSearch.Value
            Else
                DataSH.Range("AH3") = "*" & Me.txtSearch.Value & "*"
            End If
[COLOR=#008000]            'Show in the userform the header that is added[/COLOR]
            Me.txtAllColumn = DataSH.Range("AH2").Value
        End If
    End If
[COLOR=#008000]    '////////////////////////////////////////////////[/COLOR]
[COLOR=#008000]  [/COLOR]
[COLOR=#008000]    [/COLOR]
[COLOR=#008000]    'Filter the data[/COLOR]
    DataSH.Range("B4").CurrentRegion.AdvancedFilter Action:=xlFilterCopy, _
    CriteriaRange:=Range("Data!$AH$2:$AH$3"), CopyToRange:=Range("Data!$AJ$4:$BA$4"), _
    Unique:=False
[COLOR=#008000]    'Add the dynamic data to the listbox[/COLOR]
    lstClient.RowSource = DataSH.Range("Outdata").Address(external:=True)
   
[COLOR=#008000]    'Error handler[/COLOR]
    On Error GoTo 0
    Exit Sub
errHandler:
  
[COLOR=#008000]    'If error occurs then show me exactly where the error occurs[/COLOR]
    MsgBox "No Match found for " & txtSearch.Text
[COLOR=#008000]    'Clear the [/COLOR][COLOR=#008000]listbox[/COLOR][COLOR=#008000] if no match is found[/COLOR]
    Me.lstClient.RowSource = ""
    Exit Sub
End Sub
[CODE]
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Forgot to mention that there is a secondary macro that works alongside this one.

It's triggered when the ComboBox containing the headers is changed.


Code:
Private Sub cmdHeader_Change()
[COLOR=#008000]'Dim the variable[/COLOR]
    Dim DataSH As Worksheet
[COLOR=#008000]    'Set the variable[/COLOR]
    Set DataSH = Sheet4
[COLOR=#008000]    'Establish the condition for "All Columns"[/COLOR]
    If Me.cmdHeader.Value = "All Columns" Then
        DataSH.Range("AH2") = ""
    Else
[COLOR=#008000]        'Clear the textbox[/COLOR]
        Me.txtAllColumn = ""
[COLOR=#008000]        'Add teh criteria header to the sheet[/COLOR]
        DataSH.Range("AH2") = Me.cmdHeader.Value
[COLOR=#008000]        'Clear any existing criteria[/COLOR]
        DataSH.Range("AH3") = ""
    End If
End Sub
[CODE]
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,728
Members
448,987
Latest member
marion_davis

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