Sub FilterDelimitedData()
Const WS_NAME As String = "Sheet1"
Const CRITERIA_COLUMN As Long = 20
Const CRITERION As String = "tichava"
Const CRITERIA_DELIMITER As String = ", "
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Dim ws As Worksheet: Set ws = wb.Sheets(WS_NAME)
If ws.FilterMode Then ws.ShowAllData
If ws.AutoFilterMode Then ws.AutoFilterMode = False
Dim rg As Range: Set rg = ws.Range("A1").CurrentRegion
Dim Data():
Data = rg.Columns(CRITERIA_COLUMN).Resize(rg.Rows.Count - 1).Offset(1).Value
Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
dict.CompareMode = vbTextCompare
Dim SubStrings() As String, r As Long, rStr As String
For r = 1 To UBound(Data)
rStr = CStr(Data(r, 1))
If Len(rStr) > 0 Then
SubStrings = Split(rStr, CRITERIA_DELIMITER)
If IsNumeric(Application.Match(CRITERION, SubStrings, 0)) Then
If Not dict.Exists(rStr) Then
dict(rStr) = Empty
End If
End If
End If
Next r
rg.AutoFilter CRITERIA_COLUMN, dict.Keys, xlFilterValues
End Sub
Public Function containsName(haystack As String, needle As String) As Boolean
Dim words() As String, i As Long
words = Split(Replace(haystack, " ", ""), ",")
For i = 0 To UBound(words)
If words(i) = needle Then
containsName = True
Exit Function
End If
Next i
End Function
2条答案
按热度按时间xxb16uws1#
过滤分隔数据
50few1ms2#
当您标记了问题
VBA
时,我假设一个小VBA函数是可以的。现在,在VBA中定义过滤器的可能性与在Excel中使用自动过滤器时完全相同-选择一个或多个您想要选择的值,或者使用高级过滤器来定义一个或两个“通配符”(但不能超过两个)过滤器,例如 * 包含 * 或 * 以 * 开头。我看不出一种方法来指定一个高级过滤器来满足您的确切需求。
首先定义一个简单的函数
现在,如果字符串包含 * 精确 * 您要查找的名称,则该函数将返回
True
。有了这个,你有两种可能性:要么在Excel中使用辅助列,将该函数用作公式中的UDF,例如
=containsName(T2, "tichava")
,然后按此列过滤数据。或者,如果辅助列不是选项,则循环遍历所有数据,并创建一个包含您想要包含的所有单元格的数组,并将该数组用作筛选器。