[英]VBA how to copy to second last cell in table?
I have a pivot table with data, I need to copy and past all of the data except for the last row, which I need to delete as it is a sum of totals. 我有一个包含数据的数据透视表,我需要复制并粘贴除最后一行以外的所有数据,因为最后一行是总和,因此我需要删除。 The amount of data entries vary by week so I cannot just put the set numbers in. Currently my code looks like this;
数据输入的数量因周而异,所以我不能只输入设置的数字。
Worksheets("Mobile Summary").Range("A4:F482").Copy
Worksheets("Sheet1").Range("A1:F479")
However I need to copy and paste more data from a different pivot table directly under this. 但是,我需要直接从另一个数据透视表中复制和粘贴更多数据。 Therefore I couldn't just set the rows to 3000 for example.
因此,我不能仅将行设置为例如3000。 I know there is a code to find the last row, although I'm coming up short as to how to go about solving this problem.
我知道有一个代码可以找到最后一行,尽管我在解决该问题的方法上有些不足。
To find a pivot table by its name and remove the last row of the table use the Range.Resize property . 要通过其名称查找数据透视表并删除表的最后一行,请使用Range.Resize属性 。 This is useful if there is more than one pivot table on a worksheet.
如果工作表上有多个枢纽分析表,这很有用。
Option Explicit
Sub CopyPivotTableWithoutLastRow()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Tabelle3")
Dim i As Long
For i = 1 To ws.PivotTables.Count 'loop throug all pivot tables
If ws.PivotTables(i).name = "PivotTable1" Then 'determine pivot table by its name
With ws.PivotTables(i).TableRange1 '= full table
.Resize(RowSize:=.Rows.Count - 1).Copy 'resize to remove last row then copy
End With
End If
Next i
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.