问题
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:
The data:
Select any cell inside the table => Insert => Pivot Table => OK
Check all the fields on the right-side pane in this order: Date, Name, Expenditures.
Right click on the month in the pivot table => Ungroup...
Right click on the date in the pivot table => Field Settings...
In the tab "Subtotals & Filters" choose "None".
- In the tab "Layout & Print" choose "Show item labels in tabular form".
- "OK".
Filter the table using the right-side pane.
The result:
来源:https://stackoverflow.com/questions/60378055/filtering-table-to-create-another-table-in-excel