如何通过公式实现Excel过滤功能?

时间:2016-01-22 15:27:59

标签: excel excel-formula

我有一个数据表,有些数据是垃圾。按单位排序的约30000个条目。我只关心其中的20个单位。我可以通过应用过滤器轻松地对数据进行排序,但单击23次左右是很繁琐的,我将不得不每周管理此报告。

我已将相关标准捕获到不同的工作表中,所有非重复值都排成一列。我想安排另一张表,这样只有当我们的单位列中的数据与条件列匹配时,它才显示我的表格中的行。

我知道我需要以某种方式使用VLOOKUP ...但我没有偶然发现任何将单元格值与表格进行比较的教程。

如果这一切都非常令人困惑:

我的表:

Action | Job Desc    | Dept         
XFR    | IT Guy      | Home Office 1
POS    | Security Guy| Satellite Office
TTL    | Analyst Guy | Home Office 2

我想要一张只包含3行的新工作表:

Action | Job Desc    | Dept         
XFR    | IT Guy      | Home Office 1
TTL    | Analyst Guy | Home Office 2

我有价值观"内政部1"和"内政部2"存储在别处(实际上有28个不同的办公室值)。如何构建此工作表,使其仅显示这些值 - 类似于库存Excel过滤器功能?

3 个答案:

答案 0 :(得分:1)

我认为最简单的方法是创建一个"有趣的"单位和vlookup到此。在新的有趣选项卡中,您将列出您对A列感兴趣的20个项目。

在包含所有30,000行的数据选项卡中,您需要添加一个新列以检查有趣选项卡中是否存在每一行。我假设单位在C列中,您在单元格D1 =NOT(ISERROR(VLOOKUP(C1,InterestingTab!A:A,1,0)))中输入此公式。

公式的结果为TRUE或FALSE,可以轻松过滤。然后,您可以轻松地将新项目添加到有趣的选项卡中,它将自动更新。

答案 1 :(得分:0)

一个非常常见的问题。

假设:

1)您正在使用Excel 2010或更高版本

2)原始表位于Sheet1!A1:C10(第1行中包含标题)

3)容纳(已过滤)结果的表格位于Sheet2,且与原始表格的布局相同

4)(28)标准列表在Sheet3!A2:A29

然后在Sheet2!J1中输入此单个公式:

=SUMPRODUCT(COUNTIF(Sheet3!A2:A29,Sheet1!C2:C10))

当然,此处单元格的选择不一定是J1,但无论您选择什么,请确保它是结果表外部的单元格。这个公式是一次性的,与主结果表中的公式不同,不是为了复制到任何其他单元格而设计的;它只是确定了预期回报的数量,并将在主表公式中引用,从而避免了资源繁重的IFERROR设置。

结果表的单元格A2中的公式是:

=IF(ROWS($1:1)>$J$1,"",INDEX(Sheet1!A:A,AGGREGATE(15,6,ROW(Sheet1!B$2:B$10)/MATCH(Sheet1!$C$2:$C$10,Sheet3!$A$2:$A$29,0)^0,ROWS($1:1))))

并根据需要向下和向右复制。

显然,如果原始表中的上排引用实际上不是10,那么您需要相应地修改这些公式中的那个部分。但是,请确保不要选择任意大的值,因为,对于每个引用的附加单元,将需要额外的计算(并且这适用于那些额外的单元是否在技术上超出了那些范围中最后使用的单元格。)

因此,我建议你为被引用的最后一行选择一个适当的低,但是足够的上限,或者更好的是,让你的范围动态,这样它们会随着你的数据扩展/收缩而自动调整。 / p>

此致

答案 2 :(得分:0)

这是我的anser ......

Sub takeMyValus()
    Dim r1
    Dim r2
    Dim c
    Dim rng1 As Range
    Dim rng2 As Range
    Dim sht1 As Worksheet
    Dim sht2 As Worksheet
    Dim sht3 As Worksheet
    Dim List()
    Dim i
    Dim j

    r1 = Range("A1").End(xlDown).Row 'to know the last row
    c = Range("A1").End(xlToRight).Column 'to know the last colum
    Set sht1 = Sheets("Data") 'this is the name I used, but you
                              'put the name of your data sheet
    Set sht2 = Sheets("List") 'the sheet with the sorted list of the data you want


    sht1.Activate 'Just in case
    Set rng1 = Range(Cells(1, 1), Cells(r1, c)) 'set just the range with data
    rng1.AutoFilter 'set the autofilter
                    'is better if the data has no autofilter
                    'when you begin to run the macro

    sht2.Activate
    'imagine that you got the list in column A and is just 5 items in your data
    'With no header
    '+---------+
    '| Office3 |
    '| Home5   |
    '| Office8 |
    '| Home8   |
    '| Sat2    |
    '+---------+
    'List for my example...


    r2 = Range("A1").End(xlDown).Row 'to know the total item on the list
                                     'in this case will be 5

    Set rng2 = Range(Cells(1, 1), Cells(r2, 1)) 'set the range of the list
                                                'that is Range("A1:A5")
    j = 0 'ini the counter

    For Each i In rng2 'for every cell in Range("A1:A5")
        j = j + 1 'increase the j to 1 every time
        ReDim Preserve List(1 To j)
        'redimension the var...
        List(j) = i.Value 'store every cell (just the data) into an array
    Next i 'next one.

    sht1.Activate 'go to sheet with all the data
    rng1.AutoFilter Field:=3, Criteria1:=Array(List), Operator:=xlFilterValues 'set the filter with the list
    rng1.SpecialCells(xlCellTypeVisible).Copy 'copy just the cells that you can see, this is the filter

    Sheets.Add after:=Sheets(Sheets.Count) 'add a new sheet
    ActiveSheet.Name = myTime 'put a diferent name, see the function below
    Set sht3 = ActiveSheet 'store the new sheet into this var

    sht3.Activate 'go to the new sheet... is already activate, but just in case...
    Range("A1").PasteSpecial xlPasteAll 'paste all in Range("A1")
    Application.CutCopyMode = False 'is like press ESCAPE in the keyboard

End Sub

Function myTime() As String 'the function a told you
    Dim HH
    Dim MM
    Dim SS
    Dim TT
    HH = Hour(Now)
    MM = Minute(Now)
    SS = Second(Now)
    myTime = Format(HH, "00") & Format(MM, "00") & Format(SS, "00")
End Function

以下是我的数据示例......

+--------+---------+----------+
| Action | Job Des |   Dept   |
+--------+---------+----------+
| XFR    | IT      | Office1  |
| POS    | Sec     | Office2  |
| TTL    | Analyst | Office3  |
| XFR    | IT      | Office4  |
| POS    | Sec     | Office5  |
| TTL    | Analyst | Office6  |
| XFR    | IT      | Office7  |
| POS    | Sec     | Office8  |
| TTL    | Analyst | Office9  |
| XFR    | IT      | Office10 |
| POS    | Sec     | Home1    |
| TTL    | Analyst | Home2    |
| XFR    | IT      | Home3    |
| POS    | Sec     | Home4    |
| TTL    | Analyst | Home5    |
| XFR    | IT      | Home6    |
| POS    | Sec     | Home7    |
| TTL    | Analyst | Home8    |
| XFR    | IT      | Home9    |
| POS    | Sec     | Home10   |
| TTL    | Analyst | Home11   |
| XFR    | IT      | Home12   |
| POS    | Sec     | Sat1     |
| TTL    | Analyst | Sat2     |
| XFR    | IT      | Sat3     |
| POS    | Sec     | Sat4     |
| TTL    | Analyst | Sat5     |
| XFR    | IT      | Sat6     |
| POS    | Sec     | Sat7     |
| TTL    | Analyst | Sat8     |
| XFR    | IT      | Sat9     |
| POS    | Sec     | Sat10    |
| TTL    | Analyst | Sat11    |
| XFR    | IT      | Sat12    |
| POS    | Sec     | Sat13    |
| TTL    | Analyst | Sat14    |
+--------+---------+----------+

列表

+---------+
| Office3 |
| Home5   |
| Office8 |
| Home8   |
| Sat2    |
+---------+

结果:

+--------+---------+---------+
| Action | Job Des |  Dept   |
+--------+---------+---------+
| TTL    | Analyst | Office3 |
| POS    | Sec     | Office8 |
| TTL    | Analyst | Home5   |
| TTL    | Analyst | Home8   |
| TTL    | Analyst | Sat2    |
+--------+---------+---------+