筛选表以在Excel中创建另一个表

时间:2020-02-24 14:30:47

标签: excel vba filter pivot pivot-table

我需要从满足条件的表中接收值。例如,我有名字,我需要从出现名字的表中获取所有值。 例如,表格由日期,名称,支出组成:

Date        Name    Expenditures
2019-01-28  Sara    2.45
2019-04-26  John    32.67
2019-05-07  Peter   55.88
2019-06-14  Sara    62.09
2019-07-03  Sara    12.94
2019-09-30  Peter   5.64

我需要接收Sara和完成之日Date的所有支出。结果应为以下内容:

Date        Name    Expenditures
2019-01-28  Sara    2.45
2019-06-14  Sara    62.09
2019-07-03  Sara    12.94

我需要将此显示为表格,因此过滤在此处不起作用。基本上,我有一个大表,如第一个代码片段中所述,我需要创建另一个结构相同的表,但只能通过Name进行过滤。

2 个答案:

答案 0 :(得分:1)

Sub test()

Dim allData As Range, criteriaRng As Range, i As Long

Set allData = Range("A1").CurrentRegion

For i = 1 To allData.Rows.Count
    If UCase(allData(i, 2)) = "SARA" Then
    If criteriaRng Is Nothing Then
        Set criteriaRng = allData.Range(Cells(i, 1), Cells(i, allData.Columns.Count))
        Else
        Set criteriaRng = Union(criteriaRng, allData.Range(Cells(i, 1), Cells(i, allData.Columns.Count)))
    End If
    End If
Next

criteriaRng.Copy
ActiveSheet.Paste Destination:=Worksheets("Sheet1").Range("A11")

Application.CutCopyMode = False

End Sub

enter image description here

答案 1 :(得分:0)

  1. 数据:
    data

  2. 选择表内的任何单元格=>插入=>数据透视表=>确定

  3. 按以下顺序检查右侧窗格上的所有字段:日期,名称,支出。 pivot fields

  4. 右键单击数据透视表中的月份=>取消分组... Ungroup date

  5. 右键单击数据透视表中的日期=>字段设置... Field settings

  6. 在“小计和过滤器”标签中,选择“无”。

  7. 在“布局和打印”标签中,选择“以表格形式显示项目标签”。
  8. “确定”。
  9. 使用右侧窗格过滤表。 filtering

  10. 结果:
    result