简体   繁体   English

VBA如何复制到表中倒数第二个单元格?

[英]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.

相关问题 Excel VBA - 如何将单元格引用更改为倒数第二列和最后一列 - Excel VBA - How to change a cell reference to the second last and last column 如何使用VBA在Excel中将单元格复制到最后一行? - How to achieve cell copy to the last row in excel using vba? 如何将Excel中的公式复制到VBA中具有数据的最后一个单元格 - How to copy down formula in Excel to last cell with data in VBA excel vba选择单元格中的倒数第二个字符 - excel vba select second to last character in a cell 如何使用VBA在Excel ListColumn的DataBodyRange中从倒数第二个单元格中选择范围 - How to select range in Excel ListColumn's DataBodyRange from second to second last cell with VBA 如何将单元格范围复制为表格从Excel到Powerpoint-VBA - How to copy cell range as table from excel to powerpoint - VBA 如何从单元格中复制并粘贴到不同表格 VBA 的新行中 - How to copy from a cell and paste in a new row of a different table VBA Excel VBA:如何将值粘贴到表的最后一行的第一个单元格中? - Excel VBA: How to paste a value in the first Cell of the last row of a table? 如何将单元格区域从Excel复制到PowerPoint - VBA - How to copy cell range as table from Excel to PowerPoint - VBA 如何使用excel vba自动复制同一工作表中最后一行的数据并将其复制到同一表的最后一行? - How to copy the data in last row and copy to the last row of a table in the same worksheet automatically using excel vba?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM