简体   繁体   中英

Copy and paste data from columns A to AI up to the last row in pivot table

I'm trying to copy and paste data from columns A and AI up to the last populated row in a pivot table that is located in columns AO to AP.

I have tried this code but it keeps pasting beyond the last row of the pivot table.

Sub Test1()

    Dim LastRow As Long

    Set blah = Range("A2:AI2")

    Sheets("FI").Activate

    LastRow = Cells(Cells.Rows.Count, "AO").End(xlUp).Row
    Range("A2:AI2" & LastRow).FillDown

End Sub

To select a field from a pivot table in VBA , you could use something like this.

Dim Wks as Worksheet, oPvt as PivotTable
Set Wks = ActiveWorksheet
Set oPvt = Wks.PivotTables([PivotTableName])

oPvt.PivotSelect Name:="[Field]", _
                    Mode:=xlDataAndLabel, _
                    UseStandardName:=True
Selection.Copy

More option of what you can select from a PivotTable is here and PivotTable.PivotSelect Method in this and show you how to select some specific data from a field.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM