2

如果我有一系列值,如何提取包含特定单词的所有值?

例如:

在此处输入图像描述

那有什么更好的吗

=IFERROR(INDEX(INDIRECT("A"&MATCH(D2,$A$2:$A$21,0)+2&":A21"),
 MATCH("*"&$C$1&"*",INDIRECT("A"&MATCH(D2,$A$2:$A$21,0)+2&":A21"),0)),"")

我更喜欢公式而不是 VBA,但也可以随意发布 VBA 解决方案。

4

1 回答 1

1

I trick I've learnt will help here and no VBA is required. Put this in D2 and drag down:

{=INDEX(A:A,
  SMALL(IF(ISNUMBER(SEARCH("*"&$C$1&"*",$A$2:$A$11)),
           ROW($A$2:$A$11),
           MAX(ROW($A$2:$A$11))+1)
        ,ROW()-1))}

Note: This is an array formula and needs to be entered with Control + Shift + Enter.

Explanation:

  1. The index(<arr>, <nbr>) will get the value of the row we find;
  2. The search(<string>, <arr>) will find all values in the array that match and is the reason for the array formula;
  3. The if function will return the row number if a match exists and if not set a default row number, in this case the last one + 1; and
  4. Most importantly the small(<arr>, <n-th>) function will now return the nth smallest row number starting, in this case the cell has to be in row 2 otherwise the -1 needs to be altered.

Non-VBA Pivots initiated by VBA:

I've found the pivot tables bring a large improvement in performance:

  1. Add your Pivot as normal but put the location of D1.
  2. You can use the in-built contains or search ability

    enter image description here

  3. Or use VBA on the Worksheet

    Private Sub Worksheet_Change(ByVal Target As Range)
    
        If Target.Address = "$C$1" Then
            With Me.PivotTables("PivotTable2").PivotFields("name")
                .ClearAllFilters
                .PivotFilters.Add Type:=xlCaptionContains, Value1:=Target
            End With
        End If
    
    End Sub
    
于 2013-04-28T01:03:47.633 回答