【发布时间】:2018-04-06 11:34:06
【问题描述】:
更新
我需要对表的 "header" 行和表的 "grand total" 行之间的数据透视表中的行进行行计数然后将计数找到的值复制到汇总表中。
标题行:
最后一行:
所以我非常需要从数据透视表的 第 4 行 - 第 133 行(在本例中为总行数为 130)进行计数,并将该值粘贴到 摘要表上的单元格 B23。
当前数据透视表布局:
我需要将计数整合到我已有的代码中,如下所示:
Option Explicit
Sub FilterPivotTable()
Dim rLastCell As Range
Dim PvtTbl As PivotTable
Dim ws1 As Worksheet, ws2 As Worksheet
Dim LastRow1 As Long
Set ws1 = ActiveWorkbook.Sheets("PivotTable")
Set ws2 = ActiveWorkbook.Sheets("Summary")
LastRow1 = ws1.Cells(Rows.count, 1).End(xlUp).Row
'Total - This inserts the word Total in cell B22 and pastes the total value
'found in the grand total line of the of the pivot table, in the summary sheet
'in line C22
Application.ScreenUpdating = False
ws1.PivotTables("P1").ManualUpdate = True
ws1.PivotTables("P1").ManualUpdate = False
Application.ScreenUpdating = True
With ws1.PivotTables(1).TableRange1
Set rLastCell = .Cells(.Rows.count, .Columns.count)
Set PvtTbl = Worksheets("PivotTable").PivotTables("P1")
rLastCell.Copy
With ws2
ws2.Cells(LastRow1 + 22, 3).PasteSpecial xlPasteValues
.Range("$B$22").Value = "Total"
End With
End With
'Microsoft Windows - This filters the table by any vulnerabilities that have
'the words Microsoft Windows in their description, then it inserts the words
'Microsoft Windows in cell B2 and the grand total count of this filter
'in cell C2
Application.ScreenUpdating = False
ws1.PivotTables("P1").ManualUpdate = True
ws1.PivotTables("P1").PivotFields(" Vulnerability Name").ClearAllFilters
ws1.PivotTables("P1").PivotFields(" Vulnerability Name").PivotFilters. _
Add Type:=xlCaptionContains, Value1:="Microsoft Windows"
ws1.PivotTables("P1").ManualUpdate = False
Application.ScreenUpdating = True
With ws1.PivotTables(1).TableRange1
Set rLastCell = .Cells(.Rows.count, .Columns.count)
Set PvtTbl = Worksheets("PivotTable").PivotTables("P1")
rLastCell.Copy
With ws2
.Cells(LastRow1 + 2, 3).PasteSpecial xlPasteValues
.Range("$B$2").Value = "Microsoft Windows"
End With
End With
ws1.PivotTables("P1").PivotFields(" Vulnerability Name").ClearAllFilters
'Microsoft Office- This filters the table by any vulnerabilities that have
'the words Microsoft Office in their description, then it inserts the words
'Microsoft Windows in cell B3 and the grand total count of this filter
'in cell C3
Application.ScreenUpdating = False
ws1.PivotTables("P1").ManualUpdate = True
ws1.PivotTables("P1").PivotFields(" Vulnerability Name").PivotFilters. _
Add Type:=xlCaptionContains, Value1:="Microsoft Office"
ws1.PivotTables("P1").ManualUpdate = False
Application.ScreenUpdating = True
With ws1.PivotTables(1).TableRange1
Set rLastCell = .Cells(.Rows.count, .Columns.count)
Set PvtTbl = Worksheets("PivotTable").PivotTables("P1")
rLastCell.Copy
With ws2
ws2.Cells(LastRow1 + 3, 3).PasteSpecial xlPasteValues
.Range("$B$3").Value = "Microsoft Office"
End With
End With
ws1.PivotTables("P1").PivotFields(" Vulnerability Name").ClearAllFilters
End Sub
到目前为止,我有这段代码可以进行计数:
lRowCount = ActiveSheet.PivotTables("P1").TableRange1.Rows.Count
但不确定在哪里集成以及如何复制 lRowCount 值。
这个行数需要在每个With代码块中完成。因此,当对 Microsoft Office 的漏洞名称进行过滤时,我还需要对过滤后的数据进行行计数。
【问题讨论】:
-
我很困惑。您正在更改数据透视字段,而不是过滤给定字段中可见的内容,不是吗?
-
该死,现在我也很困惑.. 其目的是有效地应用了两个过滤器 - 1)在第一个参数上过滤,然后 2)在第一个参数的“过滤”结果中过滤.我用一个例子更新了代码
-
这些是页面字段吗?你能展示你的透视布局吗?如果您尝试执行其中一些操作,您的宏记录器会记录什么?
-
您能否发布您的数据透视表和数据的屏幕截图 - 如果可能只是为了提供一个想法,请缩小范围?
-
我已经显着更新了原始帖子@SJR - 我希望现在更有意义?很遗憾,我无法发布实际数据的屏幕截图,但我希望我提供的内容足以让您帮助我。