简体   繁体   English

从一个工作表复制动态数据范围(来自数据透视表)并粘贴到另一个工作表

[英]Copy dynamic data range (from PivotTable) from one worksheet and paste to another sheet

I have a macro button on worksheet2, and my data (PivotTable) on worksheet1.我在工作表 2 上有一个宏按钮,在工作表 1 上有我的数据(数据透视表)。 I want to:我想要:

  1. Push macro-button on worksheet2按下工作表2上的宏按钮
  2. Macro clears PivotTable and selects a certain field on worksheet1宏清除数据透视表并选择 worksheet1 上的某个字段
  3. Copy the PivotTable on worksheet1复制 worksheet1 上的数据透视表
  4. Paste to a specific cell on worksheet2粘贴到 worksheet2 上的特定单元格

My macro does everything but it does not copy the data from worksheet1, it copy's the data from worksheet2 for some reason.我的宏做了所有事情,但它不复制工作表 1 中的数据,它出于某种原因复制了工作表 2 中的数据。 I have the code within a With statement, not sure why it's copying data from the activesheet (worksheet2).我在 With 语句中有代码,不知道为什么它从活动表(工作表 2)复制数据。

Sub muniButton()

Application.ScreenUpdating = False

' Refresh PivotTable and copy source data
    
    With worksheet1
        .PivotTables("inventoryPivot").ClearAllFilters
        .PivotTables("inventoryPivot").PivotFields("Type"). _
            CurrentPage = "REGIONAL"
        Range("P5:Q5", Range("P5:Q5").End(xlDown)).Copy
    End With
    
' Pasting source data

    worksheet2.Range("outputCorner").Offset(1, 0).PasteSpecial
    Application.CutCopyMode = False

Application.ScreenUpdating = True   

End Sub

I'm not including .我不包括. in front Range("P5:Q5", Range("P5:Q5").End(xlDown)).Copy because it gives an error.在前面Range("P5:Q5", Range("P5:Q5").End(xlDown)).Copy因为它给出了错误。 Shouldn't this copy data from worksheet1 since it's within the With statement?这不应该从 worksheet1 复制数据,因为它在 With 语句中吗? For some reason it copys the data from worksheet2 since it's the activesheet.由于某种原因,它从 worksheet2 复制数据,因为它是活动表。

Thanks.谢谢。

If you want to copy the entire pivot table you can use it's TableRange1 property.如果要复制整个 pivot 表,可以使用它的 TableRange1 属性。

    With worksheet1.PivotTables("inventoryPivot")
        .ClearAllFilters
        .PivotFields("Type").CurrentPage = "REGIONAL"
        .TableRange1.Copy
    End With

Note, if you have page fields and you also want to copy those replace TableRange1 with TableRange2.请注意,如果您有页面字段并且您还想复制这些字段,请将 TableRange1 替换为 TableRange2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将数据透视表范围从一个工作表复制到另一个工作表。 代码正在复制活动工作表数据 - Copy PivotTable range from one worksheet to another worksheet. Code is copying active sheet data instead 将动态范围从一个工作表复制并粘贴到另一个工作表 - Copy and Paste Dynamic Range from One Worksheet to Another 将粘贴范围从一个工作表复制到另一个 - Copy paste range from one worksheet to another 将范围从一张纸复制/粘贴到另一张纸 - Copy/Paste Range from one sheet to another Excel宏可将数据从一个工作表复制并粘贴到另一工作表 - Excel macro to copy and paste data from one worksheet to another worksheet 将粘贴数据从一张工作表复制到任何其他工作表 - Copy paste data from one sheet to any other worksheet 将选定的动态数据列从一张表复制并粘贴到另一张表 - Copy and paste selected dynamic data columns from one sheet to another sheet 如何创建循环以将数据从一个工作表复制并粘贴到另一个工作表 - How to create a loop to Copy and Paste Data from one worksheet to another 从一张纸到另一张纸复制并粘贴一系列单元格,然后清除原始单元格中的数据 - Copy and paste a range of cells from one sheet to another then clear data from orignal cells 从范围循环中的每个单元格复制数据并将其粘贴到另一张纸上 - copy data from each cell in range loop and paste it on another sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM