Filtering table to create another table in Excel

非 Y 不嫁゛ 提交于 2021-01-29 10:17:13

问题


I need to receive values from a table where a condition is met. For example I have names and I need to take all values from a table where the name appears. For example the table consists of date, name, expenditures:

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

I need to receive all expenditures which has been made by Sara and the Date days when it was done. The result should be the following:

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

I need this to appear as a table, so filtering does not do the job right here. Basically I have a large table as mentioned in the first code snippet and I need to create another table with the same structure but only filtering by the Name.


回答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




回答2:


  1. The data:

  2. Select any cell inside the table => Insert => Pivot Table => OK

  3. Check all the fields on the right-side pane in this order: Date, Name, Expenditures.

  4. Right click on the month in the pivot table => Ungroup...

  5. Right click on the date in the pivot table => Field Settings...

  6. In the tab "Subtotals & Filters" choose "None".

  7. In the tab "Layout & Print" choose "Show item labels in tabular form".
  8. "OK".
  9. Filter the table using the right-side pane.

  10. The result:



来源:https://stackoverflow.com/questions/60378055/filtering-table-to-create-another-table-in-excel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!