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!
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]