如何让Pivot表使用动态数据集中的数据?

时间:2017-02-11 06:19:38

标签: vba excel-vba excel

我试图让VBA代码识别一个可以改变的数据集,与设置范围相比,如Range(A1:C100)

我试图为了自动化pivottable

而这样做

据我所知,我们可以找到此数据集的最后一列和最后一行,分别为3和6.我想知道是否可以使用此信息来使用我想要的范围?

具体来说,我认为需要这些步骤:

  • 1)能够将Column(3)更改为字母表,以便VBA能够理解它
  • 2)能够将最后一行和最后一次col数据转换为RANGE表格。

代码

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("Record").Select 
Range("A3").Select

' Create the PivotTable object based on the Employee data on Sheet1/record
Set objTable = Sheet1.PivotTableWizard

' Specify row and column fields.
Set objField = objTable.PivotFields("Outcome")
objField.Orientation = xlRowField
Set objField = objTable.PivotFields("Broker Group")
objField.Orientation = xlColumnField

' Specify a data field with its summary ' function and format.
Set objField = objTable.PivotFields("Outcome")
objField.Orientation = xlDataField
objField.Function = xlCount
objField.NumberFormat = "#,##0"

' Specify a page field. '
' Set objField = objTable.PivotFields("GENDER")
' objField.Orientation = xlPageField

' 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

示例:

enter image description here

0 个答案:

没有答案