我是VBA的新手,也是这个群体的新成员,如果我问一些愚蠢的问题,请原谅我。我正在尝试编写VBA编程枢轴表,以了解有多少成员被声明为该项目的成员。
我将获得帐户ID,它将出现在报表筛选器中,可以是一个或多个类似帐户ID的"AI124", "AI245", "AI456", "AI671",来自帐户ID的100个( 50K记录)。
1)我无法使用VBA过滤多个帐户ID。
2)团队成员会从去年开始在这个帐户上声明ID,但是我需要从3月11日开始在头中指定值。
我试着登录互联网,并开始写下面的代码,这可能是不正确的,但作为参考。
Sub CreatePivot()
' Creates a PivotTable report from the table on Sheet1
' by using the PivotTableWizard method with the PivotFields
' method to specify the fields in the PivotTable.
Dim objTable As PivotTable, objField As PivotField
' Select the sheet and first cell of the table that contains the data.
ActiveWorkbook.Sheets("DATA").Select
Range("A1").Select
' Create the PivotTable object based on the Employee data on Sheet1.
Set objTable = Sheet1.PivotTableWizard
' Specify row and column fields.
Set objField = objTable.PivotFields("Emp Last Name")
objField.Orientation = xlRowField
objField.Position = 1
Set objField = objTable.PivotFields("Emp Ser Num")
objField.Orientation = xlRowField
objField.Position = 2
Set objField = objTable.PivotFields("Week Ending Date")
objField.Orientation = xlColumnField
objField.Position = 1
' Specify a data field with its summary
' function and format.
Set objField = objTable.PivotFields("Total Hrs Expended")
objField.Orientation = xlDataField
objField.Function = xlSum
objField.NumberFormat = "#,##0"
' Specify a page field.
Set objField = objTable.PivotFields("Account Id")
objField.Orientation = xlPageField
Application.ScreenUpdating = False
'ActiveSheet.PivotTables("PivotTable2").ManualUpdate = True
objTable.ManualUpdate = True
'objTable.PivotFields("Account Id").ClearAllFilters
' objTable.PivotFields("Account Id").CurrentPage = Array("PJ1234", _ "AI82159", "AI5444")
'-------------
Set objField = objTable.PivotFields("Account Id")
objField.Orientation = xlPageField
Myarray = Array("PJ1234", "AI5444", "AI82159")
For x = 1 To objField.PivotFields.Count
objField.PivotFields(x).Visible = False
Next
For x = 0 To 3
objField.PivotFields(Myarray(x)).Visible = True
Next
objTable.ManualUpdate = False
Application.ScreenUpdating = True
' Preview the new PivotTable report.
ActiveSheet.PrintPreview
' Prompt the user whether to delete the PivotTable.
Application.DisplayAlerts = False
If MsgBox("Delete the PivotTable?", vbYesNo) = vbYes Then
ActiveSheet.Delete
End If
Application.DisplayAlerts = True
End Sub非常感谢你的帮助。
发布于 2016-05-03 06:01:54
我还没有测试过这个答案中的任何评论。
小事情-可能不会对你的问题造成什么影响.
数据透视表真的在Sheet1上吗?无论如何,我建议修改这个..。
Set objTable = Sheet1.PivotTableWizard..。就像这样..。
Set objTable = Worksheet("Sheet1").PivotTables("PivotTable1")您可以在分析带上找到PivotTable名称。
看起来,您使用的是PivotFields,而您应该在那里使用PivotItems。我会改变这个..。
objField.PivotFields(x).Visible = False..。为了这个..。
objField.PivotItems(x).Visible = False..。改变这个..。
objField.PivotFields(Myarray(x)).Visible = True..。为了这个..。
objField.PivotItems(Myarray(x)).Visible = Truehttps://stackoverflow.com/questions/36994702
复制相似问题