【问题标题】:How to re-run recorded pivot table macro?如何重新运行记录的数据透视表宏?
【发布时间】:2019-09-02 18:19:43
【问题描述】:

我录制了一个宏来从一个数据源获取数据透视表,并将代码分配给一个按钮。

代码在我第一次执行宏时有效

当我第二次重新运行宏时,它给出了一些运行时错误。我知道表目标范围中已经存在表的原因。如何使我的宏使按钮仅工作一次或仅在源数据表中的数据发生更改时工作?

示例代码

Sub pivotexample()

    Dim pt As PivotTable
    Sheets("pivot_view").Select
    For Each Pt In Sheets("pivot_view").PivotTables
        Sheets("pivot_view").Range(PT.TableRange2.Address).Delete Shift:=xlUp
    Next PT

    Sheets("result").Select
    Cells.Select
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "result!R1C1:R1048576C26", Version:=xlPivotTableVersion14).CreatePivotTable _
        TableDestination:="pivot_view!R8C1", TableName:="PivotTable1", DefaultVersion _
        :=xlPivotTableVersion14
    Sheets("pivot_view").Select
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("week_name")
        .Orientation = xlPageField
        .Position = 1

        'code for all the pivot table fields.

    End With

    Set pt = Worksheets("pivot_view").PivotTables("PivotTable1")

    Range("A8").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlToLeft)).Select
    Range("A8:I8").Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    ActiveSheet.UsedRange.Copy
    Range("A9").Offset(pt.TableRange1.Rows.Count, 0).Select
    ActiveSheet.Paste
    With ActiveSheet.PivotTables("PivotTable2").PivotFields("task_queue_name")  'getting error in this line, when i run the code for once, it is working fine, the name of the pivotTable2 would be pivotTable2 only, but when i remove the old pivot tables using the loop you have suggested and run that for the second time, the name of the second pivot table is getting changed.
        .Orientation = xlRowField
        .Position = 2
    End With
end sub

【问题讨论】:

  • 请贴出代码
  • 嗨@andreas Hofmann,谢谢,我刚刚发布了示例代码,您可以检查一次吗?

标签: excel vba outlook


【解决方案1】:

请详细说明您的问题,因为您的问题实际上由多个问题组成,例如:

  • 如何消除运行时错误?
  • 如何让我的宏只运行一次?
  • 如何检查 Range 中的数据是否发生了变化?

我将您的问题解释为您只是希望错误消失。 因此,您可以简单地删除旧表并用新表替换它。

像这样:

Sub test()

Sheets("result").Select    'result is source data sheet
Cells.Select
Sheets("pivot_view").Select  'pivot_view is my destination sheet where my pivot table should be placed

'============
'delete previous pivot table, to allow a new pivot table to get created in the same place
'code snippet source https://www.mrexcel.com/forum/excel-questions/518955-vba-delete-pivot-table.html
'code snippet autor Zack Barresse
For Each PT In Sheets("pivot_view").PivotTables
    If PT.name = "PivotTable1" then Sheets("pivot_view").Range(PT.TableRange2.Address).Delete Shift:=xlUp
Next PT
'============

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    "result!R1C1:R1048576C26", Version:=xlPivotTableVersion14).CreatePivotTable _
    TableDestination:="pivot_view!R8C1", TableName:="PivotTable1", DefaultVersion _
    :=xlPivotTableVersion14
Sheets("pivot_view").Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields("week_name")
    .Orientation = xlPageField
    .Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("team_manager")
    .Orientation = xlPageField
    .Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields( _
    "vertical_or_sub_initiative")
    .Orientation = xlRowField
End With
End Sub

当前代码将删除工作表“pivot_view”上的所有数据透视表。如果您只想创建和删除特定的,试试这个:

Sub recreate_pivot()
    Call delete_pvt("pivot_view", "PivotTable1")
    Call test("result", "pivot_view", "PivotTable1", "A1:C3", "A8")

    Call delete_pvt("pivot_view", "PivotTable2")
    Call test("result", "pivot_view", "PivotTable2", "A1:C3", "D8")

End Sub


Sub delete_pvt(pvt_sheet As String, pvt_name As String)

'============
'delete previous pivot table, to allow a new pivot table to get created in the same place
'code snippet source https://www.mrexcel.com/forum/excel-questions/518955-vba-delete-pivot-table.html
'code snippet autor Zack Barresse
'============

For Each PT In Sheets(pvt_sheet).PivotTables
    If PT.Name = pvt_name Then Sheets(pvt_sheet).Range(PT.TableRange2.Address).Delete Shift:=xlUp
Next PT

End Sub

Sub test(source_sheet As String, pvt_sheet As String, pvt_name As String, source_rng As String, dest_rng_str As String)

Dim dest_rng As Range
Set dest_rng = Sheets(pvt_sheet).Range(dest_rng_str)

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    source_sheet & "!" & source_rng).CreatePivotTable _
    TableDestination:=dest_rng, TableName:=pvt_name
Sheets(pvt_sheet).Select

With ActiveSheet.PivotTables(pvt_name).PivotFields("week_name")
    .Orientation = xlPageField
    .Position = 1
End With

With ActiveSheet.PivotTables(pvt_name).PivotFields("team_manager")
    .Orientation = xlPageField
    .Position = 1
End With

With ActiveSheet.PivotTables(pvt_name).PivotFields( _
    "vertical_or_sub_initiative")
    .Orientation = xlRowField
End With

End Sub

【讨论】:

  • 非常感谢,你明白我想要什么,你做到了,效果很好。你能检查我的其他疑问吗? stackoverflow.com/questions/57759977/…
  • 你建议的循环,当只有一个数据透视表时效果很好,但当有两个数据透视表时它不起作用,我总是在第二个数据透视表上出错,名称第一个数据透视表的名称是不变的,即数据透视表 1,但是每次我将代码运行到数据透视表 2、数据透视表 3、数据透视表 4 时,第二个数据透视表的名称都会发生变化。仅供参考,当我创建宏时,我首先创建了一个数据透视表和我复制了第一个数据透视表并粘贴在同一张表中,并在其中添加了另一个字段,我将在问题中发布代码,您可以检查一次吗?
  • 查看答案@srikanth v 中的编辑,很高兴我能提供帮助。请更具体。究竟会发生什么?什么应该发生但没有发生?
  • 我想删除两个数据透视表,我将从头开始解释,我创建了一个宏,它从一个数据源提供两个数据透视表,当我第一次运行代码时,我得到了两个数据透视表,现在如果我再次单击运行按钮,两个旧数据透视表都应该消失并用新数据替换它,但它没有发生,如果我按 f8 按钮并检查代码,我将获得第一个数据透视表表,我在第二个数据透视表上收到错误,因为我不知道为什么,第一个数据透视表名称相同(数据透视表 1),但第二个 PT 的名称从 pt2、pt3 更改。跨度>
  • 好的,明白了。您是否有理由再次创建他的数据透视表?因为您可以简单地编写一个宏来更新两个数据透视表的源数据。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多