【问题标题】:How do I select data from the excel pivot table?如何从 excel 数据透视表中选择数据?
【发布时间】:2019-07-11 15:21:04
【问题描述】:

数据透视表

例如,当我单击数据透视表中的单元格G14 时。它应该在另一张表中显示9 data,比如Sheet1。数据的数量取决于数据透视表的column G 中的单元格。我似乎找不到任何代码来获取该数据。

Sheet1

我需要知道如何使用 VBA 访问这些数据。

【问题讨论】:

  • 什么是“9数据”?

标签: excel vba pivot-table


【解决方案1】:
Sub SelectAndCopyPaste()
Dim lastrow() As String
Dim pt As PivotTable


Worksheets("Sheet1").Activate
ActiveSheet.Cells.Clear

With Worksheets("Pivot Sheet")
    .Activate
    Set pt = .PivotTables("PivotTable2")
    lastrow = Split(pt.RowRange.Address, ":")
    .Range(lastrow(1)).Offset(-1, 6).Select 'here is where you set which cell you want to choose
    Selection.ShowDetail = True
    Selection.Copy
End With

    Worksheets("Sheet1").Select
    ActiveSheet.Cells(1, 1).PasteSpecial xlPasteValues

End Sub

【讨论】:

  • 你应该避免在你的代码中使用Select
  • @Mikku 你有什么其他的建议或方法可以让这件事变得更好吗?
  • 只是避免使用选择和激活。正如您已经指的是正确的对象。
猜你喜欢
  • 1970-01-01
  • 2020-11-30
  • 2021-08-21
  • 2012-05-14
  • 1970-01-01
  • 2017-05-24
  • 1970-01-01
  • 1970-01-01
  • 2018-07-06
相关资源
最近更新 更多