【发布时间】:2015-10-27 22:11:38
【问题描述】:
我正在尝试在两个不同工作表的两个不同数据透视表中比较和过滤每周分组的日期。我想比较两个表中的日期,如果它们相同,则复制该分组日期并将其放入另一个工作表中。我拥有的 VBA 代码正在比较一个月内的所有日期。 例如:
pivot table 1 pivot table 2
10/15/2013 - 10/21/2013 10/15/2013-10/21/2013
10/22/2013 - 10/28/2013 10/22/2013 - 10/28/2013
5/27/2014 - 6/2/2014 6/3/2014 - 6/9/2014
当我运行 VBA 时,我想将前两组日期复制到另一个工作表中,因为它们是相同的,而忽略第三组,因为它们不是。每个表中的日期数可能不同。这是我到目前为止的代码
Sub Find()
Dim Pvt1 As PivotTable
Dim Pvt2 As PivotTable
Dim pf1 As PivotField
Dim pf2 As PivotField
Dim pi1 As PivotItem
Dim pi2 As PivotItem
Dim cell As Range
Set Pvt1 = ActiveWorkbook.Worksheets("Total Bloodhound").PivotTables("PivotTable3")
Set Pvt2 = ActiveWorkbook.Worksheets("Total Closed").PivotTables("PivotTable1")
Set pf1 = Pvt1.PivotFields("time")
Set pf2 = Pvt2.PivotFields("time")
Dim index As Integer
index = 1
For Each pi1 In pf1.PivotItems
For Each pi2 In pf2.PivotItems
If IsEmpty(pi2.Value) Then Exit For
If pi1.Value = pi2.Value Then
Worksheets("Sheet1").Cells(index, "A") = pi1.Value
index = index + 1
End If
Next pi2
Next pi1
End Sub
此代码比较并复制所有带有月份的日期,即使这些日期不在数据透视表中。 任何帮助都会非常感谢!
【问题讨论】:
-
你试过提供的答案了吗?
标签: vba pivot-table