[英]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:
我想要:
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.