如何根据这个标准在Excel中过滤行

我有8行像这样用逗号分隔:

ID Code Cost 1 X 20 2 20 3 50 4 50 5 Y 10 6 10 7 70 8 70 

标准是:

如果存在代码列中的值,则在没有任何操作之后打印行和行。

你怎么能在Excel中做任何数量的行?

注意订单总是相同的,所以你可以假设如果一个代码被发现的行和后面的将始终打印。 在上面的例子中,我得到的输出是:

 ID Code Cost 1 X 20 2 20 5 Y 10 6 10 

假设1,X,20在单元格A2

在单元格B2中复制下面的代码:

 =IF(OR(IF(RIGHT(LEFT(A2,SEARCH(",",A2,1)),1)=MID(A2,SEARCH(",",A2,1)+1,1),FALSE,TRUE),AND(B1=A1,B1<>"",IF(A1<>"",IF(RIGHT(LEFT(A1,SEARCH(",",A1,1)),1)=MID(A1,SEARCH(",",A1,1)+1,1),FALSE,TRUE),FALSE))),A2,"") 

C2单元格中input以下代码:

 =IF(B2="","",MAX(C$1:C1)+1) 

在单元格D2input以下代码,而不是 Enter键 ,然后按Ctrl + Shift + Enter

 =IFERROR(INDEX($B$2:$B$9,MATCH(ROW()-ROW(D$1),C$2:C$9,0)),"") 

将单元格B2C2D2复制到第9行。

您所需的输出将在D列 – 隐藏B列和C列(如果需要)。

之前:

autofilter_before

运行这个代码:

 Option Explicit Sub specialFilter() Dim a As Long, aARRs As Variant, dKEYs As Object Set dKEYs = CreateObject("Scripting.Dictionary") dKEYs.CompareMode = vbTextCompare With Worksheets("Sheet2") If .AutoFilterMode Then .AutoFilterMode = False With .Cells(1, 1).CurrentRegion 'build a dictionary so the keys can be used as the array filter aARRs = .Range(.Cells(2, 1), .Cells(.Rows.Count, 2)).Value2 For a = LBound(aARRs, 1) To UBound(aARRs, 1) If CBool(Len(aARRs(a, 2))) Then dKEYs.Add Key:=CStr(aARRs(a, 1)), Item:=aARRs(a, 1) If a < UBound(aARRs, 1) Then _ dKEYs.Add Key:=CStr(aARRs(a + 1, 1)), Item:=aARRs(a + 1, 1) End If Next a 'filter on column B if dictionary keys exist If CBool(dKEYs.Count) Then _ .AutoFilter Field:=1, Criteria1:=dKEYs.Keys, _ Operator:=xlFilterValues 'data is filtered in column A for any value in column B (and the subsequent row) 'Perform work on filtered data here End With If .AutoFilterMode Then .AutoFilterMode = False End With dKEYs.RemoveAll: Set dKEYs = Nothing End Sub 

后:

autofilter_after

好的,因为每个数据都在不同的单元格中,假设1在A2中,X在B2中,而20在C2中,

在D2中input:

= IF(OR(B2 <> “”,和(B2 = “”,B1 <> “”)),MAX(d $ 1:D1)+1 “”)

在E2中input:

= IF(COUNTBLANK($ D2)= 0,IF(A2 = “”, “”,A2), “”)

在F2中input:

= IF(COUNTBLANK($ D2)= 0,IF(B2 = “”, “”,B2), “”)

inputG2:

= IF(COUNTBLANK($ D2)= 0,IF(C2 = “”, “”,C2), “”)

H2中inputCtrl + Shift + Enter:

= IFERROR(INDEX(E $ 2:E $ 9 MATCH(ROW() – ROW(H $ 1),$ d $ 2:$ d $ 9,0)), “”)

在Ctrl + Shift + Enter中inputI2:

= IFERROR(INDEX(F $ 2:F $ 9 MATCH(ROW() – ROW(I $ 1),$ d $ 2:$ d $ 9,0)), “”)

J2中的Ctrl + Shift + Enter:

= IFERROR(INDEX(G $ 2:G $ 9 MATCH(ROW() – ROW(j $ 1),$ d $ 2:$ d $ 9,0)), “”)

将D2:J2拖到第9行并隐藏D到G列。