Vba 如何选择一些选定的透视表区域?
我正在尝试选择并复制数据透视表的某些选定区域。我能够确定我想要的面积大小,并且能够在一个不是我目标的消息框中显示范围。我想复制选定的范围。我的代码看起来像这样。 我想复制范围内的值(toprow1,LeftColumn:lastrow,RightColumn)。 仅供参考,信息框代码是我不需要的,它只是告诉你范围号Vba 如何选择一些选定的透视表区域?,vba,excel,Vba,Excel,我正在尝试选择并复制数据透视表的某些选定区域。我能够确定我想要的面积大小,并且能够在一个不是我目标的消息框中显示范围。我想复制选定的范围。我的代码看起来像这样。 我想复制范围内的值(toprow1,LeftColumn:lastrow,RightColumn)。 仅供参考,信息框代码是我不需要的,它只是告诉你范围号 Sub PivotTableRangeAreas() With ActiveSheet.PivotTables(1) Dim TopRow1 As Long,
Sub PivotTableRangeAreas()
With ActiveSheet.PivotTables(1)
Dim TopRow1 As Long, TopRow2 As Long, LastRow As Long
Dim LeftColumn As Long, RightColumn As Long
TopRow2 = .TableRange2.Row
With .TableRange1
TopRow1 = .Row
LastRow = .Rows.Count + .Row - 1
LeftColumn = .Column
RightColumn = .Columns.Count + .Column - 1
End With
MsgBox "The pivot table named " & .Name & vbCrLf & _
"occupies these range elements:" & vbCrLf & vbCrLf & _
"With the Report (Page) field: " & vbCrLf & _
.TableRange2.Address(0, 0) & vbCrLf & _
"Without the Report (Page) field: " & vbCrLf & _
.TableRange1.Address(0, 0) & vbCrLf & vbCrLf & _
"First row, with the Report (Page) field: " & TopRow2 & vbCrLf & _
"First row, without the Report (Page) field: " & TopRow1 & vbCrLf & _
"Last row: " & LastRow & vbCrLf & _
"Left column: " & LeftColumn & vbCrLf & _
"Right column: " & RightColumn, , "Pivot table location."
End With
End Sub
我猜这只是你想要复制的值?如果是这样的话,试着从这样的地方开始——它将把值放在表2中,从范围A1开始。我不确定要复制数据透视表到何处的范围-您必须更改其中一些以适应:
Sub CopyRange()
Dim vArray() As Variant
'Copies the values between (Toprow1, LeftColumn) and (LastRow, RightColumn) into an array
vArray = ActiveSheet.Range(Cells(TopRow1, LeftColumn), Cells(LastRow, RightColumn)).Value
'Pastes the values from the array into Sheet2, starting at A1
Sheet2.Range("A1").Resize(UBound(vArray, 1), UBound(vArray, 2)).Value = vArray
End Sub
如果范围不连续-我不认为它可能被复制和粘贴。@peter ya我无法复制和粘贴它。变量toprow、lastrow、firstcolumn、lastcolumn帮助我将范围缩小到我的首选范围。在pivot表中生成的报告,我通过tablerange1访问。我想在报告区域中选择第二列到第六列,并保留该报告的前两行和最后一行。然后选择“将条目复制为字符串”并将其粘贴到“我的使用”中。错误在哪里?你们有叫Sheet2的单子吗?如果没有更多的信息,我帮不了你。马特,如果你看一下我的代码,我可以告诉你我是如何使用你的代码的。我删除了写在两端之间的代码。然后从你的代码中,我将代码复制到子代码和结束子代码之间。然后将你的代码部分粘贴到我的结束子代码的正上方。这样做不起作用。请你解释一下如何使用这个。你好,尼桑-你所做的应该有用。如果您查看Sheet2,是否有任何数据粘贴到其中?有错误吗?Matt这张表2是我们给的名字,对吗?或者VBA中显示的名称。嘿,马特,很抱歉,是的,它正在工作,谢谢