dougmarkham
Active Member
- Joined
- Jul 19, 2016
- Messages
- 252
- Office Version
- 365
- Platform
- Windows
Hi Folks,
My goal is to be able to filter a column in a dynamic table for strings that contain non-alpha characters like #, $, etc using wildcards (*).
I wish to use an InputBox to specify the special character.
Something unexpected is happening:
If I directly code 'Criteria1' e.g., for # i.e.,
then the below code works.
But
If I use a user InputBox so y = "*#*" and then state
, then the macro hides all lines.
This despite confirming via
that the text strings are identical!
Here is the VBA code I'm using:
Images:
versus
Would anyone be willing to help me work out how to get this filter to work with InputBox?
Kind regards,
Doug.
P.S.
Input box shows correct criteria
My goal is to be able to filter a column in a dynamic table for strings that contain non-alpha characters like #, $, etc using wildcards (*).
I wish to use an InputBox to specify the special character.
Something unexpected is happening:
If I directly code 'Criteria1' e.g., for # i.e.,
VBA Code:
Criteria1:="*#*"
But
If I use a user InputBox so y = "*#*" and then state
VBA Code:
Criteria1:=y
This despite confirming via
VBA Code:
MsgBox y
Here is the VBA code I'm using:
VBA Code:
Sub AutoFilter_By_InputBox()
Dim lo As ListObject
Dim iCol As Long
Dim x As String: x = Application.InputBox(Prompt:="Please select character to filter by", Title:="Select Filter Character", Type:=2)
Dim y As String
y = Chr(34) & Chr(42) & x & Chr(42) & Chr(34)
MsgBox y
'Set reference to the first Table on the sheet
Set lo = Sheets("DX_Invoice").ListObjects("InvoiceTable")
'Set filter field
iCol = lo.ListColumns("Customer Reference").Index
'Clear Filters
lo.AutoFilter.ShowAllData
'All lines starting with .AutoFilter are a continuation
'of the with statement.
With lo.Range
'Contains - wrap search text in asterisks
.AutoFilter Field:=iCol, Criteria1:=y
End With
End Sub
Images:
versus
Would anyone be willing to help me work out how to get this filter to work with InputBox?
Kind regards,
Doug.
P.S.
Input box shows correct criteria