AutoFilter feat. Wild Card Replacements
- Copy the complete code into a standard module (e.g.
Module1
).
- Adjust the constants in
SearchAStore
including the workbook.
- Only run
SearchAStore
, the rest is being called.
The Code
Option Explicit
Sub SearchAStore()
Const wsName As String = "Sheet1"
Const CritAddr As String = "A1:G2"
' The following line represents the n-th column of the Filter Range,
' not necessarily the n-th column of the worksheet or the Criteria Range.
' If Filter Range would start in column "B" and you want to define
' the Last Row Column using column "B", the value remains 1.
Const LastRowCol As Long = 1
Const FirstRow As String = "A4:O4"
Dim wb As Workbook: Set wb = ThisWorkbook
' Define Filter Range.
Dim ws As Worksheet: Set ws = wb.Worksheets(wsName)
Dim rng As Range
Set rng = ws.Columns(ws.Range(FirstRow).Columns(LastRowCol).Column)
Set rng = rng.Find("*", , xlFormulas, , , xlPrevious)
If rng Is Nothing Then Exit Sub
If rng.Row < ws.Range(FirstRow).Row Then Exit Sub
Set rng = ws.Range(FirstRow).Resize(rng.Row - Range(FirstRow).Row + 1)
' Define Criteria Range.
Dim rngCrit As Range: Set rngCrit = ws.Range(CritAddr)
wrapRange rngCrit.Rows(2), "*"
rng.AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=rngCrit
wrapRange rngCrit.Rows(2), "*", , True
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Purpose: Adds or removes a string (wString) as a prefix, as a suffix '
' or as both to all string values in cells of a range. '
' Inputs '
' DataRange The Range (Object). '
' wString The Wrap String i.e. the string to be added or removed. '
' Left1Right2 0 - Wrap String will be added or removed as both, '
' prefix and suffix. '
' 1 - Wrap String will be added as prefix (in front of). '
' 2 - Wrap String will be added as suffix (behind). '
' removeWrap False - Wrap String will be added. '
' True - Wrap String will be removed. '
' Remarks: When adding, if either prefix, suffix or both already exist, '
' they will not be added. When adding or removing only one, '
' the other will not be checked if it exists. '
' Precedents: wrapString '
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub wrapRange(DataRange As Range, _
ByVal wString As String, _
Optional ByVal Left1Right2 As Long = 0, _
Optional ByVal removeWrap As Boolean = False)
Dim cel As Range, Curr As String
Dim Data As Variant: Data = DataRange.Value
If Not IsArray(Data) Then
If VarType(Data) = vbString Then
DataRange.Value = wrapString(Data, wString, Left1Right2, removeWrap)
Exit Sub
End If
End If
Dim i As Long, j As Long
For i = 1 To UBound(Data)
For j = 1 To UBound(Data, 2)
Curr = Data(i, j)
If VarType(Curr) = vbString Then
Curr = wrapString(Curr, wString, Left1Right2, removeWrap)
Data(i, j) = Curr
End If
Next j
Next i
DataRange.Value = Data
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Purpose: Adds or removes a string (wString) as a prefix, as a suffix '
' or as both to a string (aString). '
' Returns: A string. '
' Inputs '
' aString The initial string. '
' wString The Wrap String i.e. the string to be added or removed. '
' Left1Right2 0 - Wrap String will be added or removed as both, '
' prefix and suffix. '
' 1 - Wrap String will be added as prefix (in front of). '
' 2 - Wrap String will be added as suffix (behind). '
' removeWrap False - Wrap String will be added. '
' True - Wrap String will be removed. '
' Remarks: When adding, if either prefix, suffix or both already exist, '
' they will not be added. When adding or removing only one, '
' the other will not be checked if it exists. '
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function wrapString(ByVal aString As String, _
ByVal wString As String, _
Optional ByVal Left1Right2 As Long = 0, _
Optional ByVal removeWrap As Boolean = False) _
As String
Dim Curr As String: Curr = aString
Dim wLen As Long: wLen = Len(wString)
Select Case Left1Right2
Case 0: GoSub writeLeft: GoSub writeRight
Case 1: GoSub writeLeft
Case 2: GoSub writeRight
End Select
wrapString = Curr
Exit Function
writeLeft:
If StrComp(Left(Curr, wLen), wString) = 0 Then
If removeWrap Then Curr = Right(Curr, Len(Curr) - wLen)
Else
If Not removeWrap Then Curr = wString & Curr
End If
Return
writeRight:
If StrComp(Right(Curr, wLen), wString) = 0 Then
If removeWrap Then Curr = Left(Curr, Len(Curr) - wLen)
Else
If Not removeWrap Then Curr = Curr & wString
End If
Return
End Function