如何正确使用GETPIVOTDATA

问题描述 投票:0回答:1

当我尝试引用数据透视表中的特定单元格引用时遇到困难。

Dim SelectionNum As Integer
    
LastRow = ActiveSheet.PivotTables("CMAPivot").TableRange2.Rows.Count

 SelectionNum = WorksheetFunction.RandBetween(4, LastRow)
 MsgBox(GETPIVOTDATA("CLM_NR",CMAPivot!$A$[SelectionNum]))   ' this is the line I get a syntax error on

我也尝试过:

 MsgBox(GETPIVOTDATA("CLM_NR",$A$[SelectionNum])) as well as
 MsgBox(GETPIVOTDATA("CLM_NR","CMAPivot"!$A$[SelectionNum])) 

无论如何,我的单元格引用 $A$[SelectionNum] 出现语法错误(我也尝试过不带括号)

我基本上是尝试随机选择一个索赔编号以进行审计。一旦我成功引用 CLM_NR,我就会将该值写入电子表格上的单元格。

提前感谢您的帮助!

excel vba pivot-table
1个回答
0
投票
  • GETPIVOTDATA
    用于获取数据透视表中数据体区域的数据。
  • 如果我是对的,你会尝试获取行字段的随机值。
  • 行数(标题和页面字段,代码中的
    4
    )可能会有所不同。
Option Explicit

Sub demo()
    Dim SelectionNum As Long
    Dim LastRow As Long, RowCnt As Long
    Dim ColRng As Range
    With ActiveSheet.PivotTables("CMAPivot")
        Set ColRng = .TableRange2.Columns(1)
        RowCnt = ColRng.Cells.Count - .DataBodyRange.Rows.Count
    End With
    SelectionNum = WorksheetFunction.RandBetween(RowCnt + 1, ColRng.Cells.Count)
    MsgBox ColRng.Cells(SelectionNum)
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.