简体   繁体   English

将 Pivot 表总计复制到下一个空单元格

[英]Copy Pivot Table Grand Total to next empty cell

So I have a Pivot Table located in column N,O with a Grand Total row and I want to copy that grand total to the next empty cell in a list in Column "R".所以我有一个 Pivot 表,位于 N,O 列,有一个总计行,我想将该总计复制到“R”列列表中的下一个空单元格。 I've found a code that can copy the grand total to a cell but I can't make it work to copy it in the next empty cell.我找到了一个可以将总计复制到一个单元格的代码,但我无法将其复制到下一个空单元格中。

Dim rngPvt As Range, sht As Worksheet, valToFind$, gtv
Set sht = Sheets("Sheet1")
Set rngPvt = sht.PivotTables("1").RowRange
valToFind = sht.PivotTables("1").GrandTotalName
Set gtv = rngPvt.Find(valToFind, , xlFormulas)
'assumes value to the right of field name
Sheets("Sheet1").Cells(LastRow, 18).PasteSpecial xlPasteValues = gtv.Offset(, 1)

Try this..尝试这个..

Sub test()

Dim wb As Workbook
Set wb = ThisWorkbook
Dim sh As Worksheet
Set sh = wb.Sheets("Sheet1")
Dim pt As PivotTable
Set pt = sh.PivotTables("PivotTable1")
Dim FinalPvtGT As Range
Set FinalPvtGT = pt.TableRange1(pt.TableRange1.Rows.Count, _
                            pt.TableRange1.Columns.Count)
Dim targetCell As Range
Set targetCell = FinalPvtGT.Offset(, 3)

targetCell = FinalPvtGT

End Sub

Are you expecting it after the last filled cell of the column R?在 R 列的最后一个填充单元格之后,您是否期待它? If so, then replace the following line如果是这样,则替换以下行

Set targetCell = FinalPvtGT.Offset(, 3)

with

Set targetCell = Range("R" & sh.Rows.Count).End(xlUp).Offset(1, 0)

This is a code which maybe you can figure out to implement it to your code:这是一个代码,也许您可以将其实现到您的代码中:

Sub test()

Set sht = Sheets("Sheet1")
Set pt1 = sht.PivotTables(1)
Set pt2 = sht.PivotTables(2)
Range("E26").Value = pt1.GetPivotData("SUM").Value
Range("B26").Value = pt2.GetPivotData("SUM").Value

End Sub

在此处输入图像描述

the grand total needs to be copied over to the next empty row in column R总计需要复制到 R 列中的下一个空行

Oops... sorry,哎呀,对不起,
so your problem is the placement of the copied GrandTotal:).所以你的问题是复制的 GrandTotal 的位置:)。

I've found a code that can copy the grand total to a cell but I can't make it work to copy it in the next empty cell我找到了一个可以将总计复制到一个单元格的代码,但我无法将其复制到下一个空单元格中

Sheets("Sheet1").Cells(LastRow, 18).Select

If you step-run the code above, is the result of the active cell correct?如果您逐步运行上面的代码,活动单元格的结果是否正确? Which as you expected that the result of the active cell is the next empty cell.正如您所料,活动单元格的结果是下一个空单元格。

If it's not correct, then maybe you need to add .offset(1,0) before the .select .如果不正确,那么您可能需要在 .select 之前添加.select .offset(1,0) But still I don't know what is the value of your 'LastRow' variable.但我仍然不知道你的“LastRow”变量的值是多少。 If the value is the last row number in the sheet (where mine is 1048576), then maybe you need to put .end(xlup).offset(1,0)如果该值是工作表中的最后一行编号(我的是 1048576),那么您可能需要输入.end(xlup).offset(1,0)

If it's correct, I wonder what is the problem?如果它是正确的,我想知道有什么问题?

It also can be done with the code above:也可以使用上面的代码来完成:

Sheets("Sheet1").Cells(LastRow, 18).value = pt1.GetPivotData("SUM").Value

The word "SUM" is depend on what is your pivot table GrandTotal column header name. “SUM”一词取决于您的 pivot 表 GrandTotal 列 header 名称是什么。 Mine is SUM as seen in the image above.如上图所示,我的是 SUM。

This code is not depend on the row where the pivot table GrandTotal displayed.此代码不依赖于 pivot 表 GrandTotal 显示的行。 So you don't have to code to find the word for the pivot table GrandTotal first then offset(0,1) to get the value.因此,您不必先编写代码来查找 pivot 表 GrandTotal 的单词,然后再使用 offset(0,1) 来获取值。 If for example at first the GrandTotal display in row 200, and after data source change and refresh the GrandTotal display in row 1000, it will always give you the correct GrandTotal.例如,如果起初 GrandTotal 显示在第 200 行,而在数据源更改并刷新第 1000 行的 GrandTotal 显示后,它将始终为您提供正确的 GrandTotal。

Pivot Grand Total to Specified Cell Pivot 总计到指定单元

Option Explicit

Sub PivotGrandTotal()

    Const SheetName As String = "Sheet1"
    Const writeColumn As Long = 18

    Dim rng As Range          ' PivotTable's RowRange, Last Non-Empty Cell
    Dim findString As String  ' PivotTable's GrandTotalName
    ' If certain that whole number, then use "As Long" in the following line.
    Dim gTotal As Double      ' Grand Total Value
    Dim FER As Long           ' Write Row (First Empty Row)

    With ThisWorkbook.Worksheets(SheetName)
        ' Retrieve Grand Total.
        With .PivotTables(1)
            findString = .GrandTotalName
            Set rng = .RowRange
        End With
        gTotal = rng.Find(findString, , xlFormulas).Offset(, 1).Value
        ' Define writeRow.
        Set rng = .Columns(writeColumn).Find("*", , xlFormulas, , , xlPrevious)
        If Not rng Is Nothing Then FER = rng.Row + 1 Else FER = 1
        ' Write to writeCell.
        .Cells(FER, writeColumn).Value = gTotal
    End With

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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