简体   繁体   English

在数组中包含部分字符串匹配

[英]Include a partial string match in an array

I'm trying to edit my VBA code to filter for multiple values (which already exist in my worksheet) and for partial matches to also be filtered.我正在尝试编辑我的 VBA 代码以过滤多个值(这些值已存在于我的工作表中)并且也过滤部分匹配项。 This is my current code:这是我当前的代码:

ActiveSheet.Range("$A$6:$AB$9903").AutoFilter Field:=15, Criteria1:=Array( _
    "Desktop", "Monitor", "Non-Standard Desktop", _
    "Non-Standard Notebook", "Notebook"), Operator:=xlFilterValues

I would like to add in another criteria, like anything that contains the word "Headset".我想添加另一个标准,比如任何包含“耳机”一词的标准。 I tried adding in * Headset* to the array above but it did not work.我尝试将 * Headset* 添加到上面的数组中,但它没有用。 I also tried creating a new chunk of code for just words containing 'Headset', and while it did filter for those cells, it only filtered for those.我还尝试为包含“耳机”的单词创建一个新的代码块,虽然它确实过滤了那些单元格,但它只过滤了那些。

I'm not really sure what other options I have.我不太确定我还有哪些其他选择。 I checked other people's solutions but it looks like in their solutions, they're doing what I did above (filtering only for words containing that specific string).我检查了其他人的解决方案,但看起来在他们的解决方案中,他们正在做我上面所做的(仅过滤包含该特定字符串的单词)。 Any advice?有什么建议吗? Thanks!谢谢!

Using AdvancedFilter When Many Criteria With Wild Characters当许多条件带有通配符时使用 AdvancedFilter

You can use the following...您可以使用以下...

With ActiveSheet
    If .FilterMode Then .ShowAllData
    With .Range("A6:AB9903") ' with headers
        Dim DataRange As Range ' without headers
        Set DataRange = .Resize(.Rows.Count - 1).Offset(1)
        .AdvancedFilter xlFilterInPlace, .Range("AD6").CurrentRegion
    End With
    Dim VisibleDataRange As Range ' without headers
    On Error Resume Next
        Set VisibleDataRange = DataRange.SpecialCells(xlCellTypeVisible)
    On Error GoTo 0
    If Not VisibleDataRange Is Nothing Then
        ' Do what you need to do (copy, delete...), e.g.:
        Debug.Print VisibleDataRange.Address(0, 0)
    'Else ' no filtered data found
    End If
    ' Remove the filter.
    .ShowAllData
End With

... if you use the range AD6:AD12 filled with the following: ...如果您使用填充以下内容的AD6:AD12范围:

COL15
=Non-Standard Notebook   
=Monitor
=Non-Standard Desktop  
=Notebook
=Desktop
*headset*

... where you will replace COL15 with the header in O6 . ...您将在O6中将COL15替换为 header。 You will enter the values eg like this: ="=Monitor" or '=Monitor .您将输入值,例如: ="=Monitor"'=Monitor

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM