我需要从满足条件的表中接收值。例如,我有名字,我需要从出现名字的表中获取所有值。 例如,表格由日期,名称,支出组成:
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
进行过滤。
答案 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
答案 1 :(得分:0)