基于多个变量动态更新数据透视表

huangapple go评论59阅读模式
英文:

Dynamically Update a Pivot Table based on Multiple Variables

问题

大家好,

很久没有来了,首次发帖等等。

我想使用DO UNTIL循环来动态更新一组数据透视表。我有一个季度报告(我将扩展此代码以适用于年度报告,但首先要解决当前问题),其中包含多个数据透视表。我希望不是用户每个月都要去更新所有7个数据透视表中的月份,而是让VBA为他们完成这项任务。

为了更新报告,我有一个数据验证列表,用户选择当前月份(例如:六月的第一周,用户会选择2023年5月)。由于五月是季度的第二个月,报告需要使用四月和五月的数据更新每个数据透视表。我有一个IF语句来确定这一点,并列出一个动态命名的范围中所需的月份(即,随着季度的进展,范围会增加,直到重置到下一个季度)。

所以让我们继续以五月为例。对于每个数据透视表,更新的格式是YYYYMM。因此,在我选择2023年5月的命名范围中,我将有202304(在Excel中命名为CQTRVLE01)和202305(CQTRVLE02)。我试图弄清楚的是如何使用DO UNTIL循环来动态循环遍历这些值并更新数据透视表。我有一个单元格值来确定季度中的当前月份号码。我希望我的循环能够识别到这是季度中的第二个月,并且只提取四月(202304)和五月(202305)的值。我不能有空值,因为数据透视表不会更新。因此,即使我有一个六月的单元格,但在到七月时我不需要它,那时我才需要它。

我用来分配值以更新数据透视表的变量是CQV0i,其中i = 季度中的月份号码。因此,由于季度中有2个月,i = 2,我需要将202304分配给CQV01,将202305分配给CQV02。此外,一旦变量已被分配,它需要更新数据透视表到月份的数量。我们使用的数据透视表非常基本,它根据您需要的值进行更新。因此,对于我有的2个值,我的数据透视表代码将如下所示:

With PT1
     ActiveSheet.PivotTables("BaseCQ").PivotFields( _
        "[Fiscal Period].[Fiscal Year - Month].[Month]").VisibleItemsList = Array( _
        "[Fiscal Period].[Fiscal Year - Month].[Month].[&1]&[" & CQV01 & "]", _
        "[Fiscal Period].[Fiscal Year - Month].[Month].[&1]&[" & CQV02 & "]", _
        "[Fiscal Period].[Fiscal Year - Month].[Month].[&1]&[" & CQV03 & "]")
End With

每次季度的月份号码增加时,我需要添加一行。因此,如果我们在四月,我只需要第一行/CQV0i = CQV01。五月我需要两行CQV01和CQV02。六月我需要所有3个CQB01、CQV02和CQV03。我不能使用空值来更新数据透视表,否则会出错。所以基于季度中的月份号码,我只需要代码中对应数量的行。

这是我迄今为止写的内容。

Dim PT1 As PivotTable
Dim x As Long
Dim i As Range
Dim CQV0i As Long

x = 1
Set i = Range("CurrRAngeNum")
Set PT1 = Worksheets("FDW_BaseCY").PivotTables("FDW_BaseCY")

Do Until x = i
Set CQV0i = Range("CQTRVLE0" & i)
我需要在这里添加一行来更新CQV0i并分配每个月的值

数据透视表代码从PT1到PT7

x = x + 1
Loop

它不允许我使用CQV0i(我得到“Object Required”错误)。我一直在搜索不同的方法,看看我如何处理这个问题,但是到这里为止我还没有找到解决办法。我知道这很复杂(至少对我来说是这样),但我会感激任何帮助或指导。如果有问题,我会继续刷新此页面,但我仍然会寻找答案,因此回复可能会有延迟。谢谢。

一直无法让VBA认识我的动态变量。一直出现“Object Required”错误。

英文:

Good Day All,

long time first time etc.

I am looking to use a DO UNTIL loop to dynamically update a group of Pivot Tables. I have a QTD report (I am going to have to expand this code to work for a YTD report, but first things first) with multiple Pivot Tables. Instead of the user, every month, going to all 7 PTs and updating the month I want VBA to do it for them.

In order to update the report I have a data validation list where the user selects the current month (ie: first week of June, user would select May 2023). Since May is the 2nd month of the quarter, the report will need to update each PT with April and May #s. I have an IF Statement that determines this and lists out the months required in a dynamic named range (ie, as the quarter progresses the range increases until it resets to the next quarter).

So lets continue w/ my May example. For each PT, the format to update is YYYYMM. So in my named range for May 2023 selection I would have 202304 (CQTRVLE01 named range in Excel) and 202305 (CQTRVLE02). What I am trying to figure out is how can I use a DO UNTIL loop do dynamically cycle through the values and update the PT. I have a cell value that determines the current month # in the quarter. I want my loop to recognize that it is the second month and only pull the values for April (202304) and May (202305). I cannot have a blank value because the PT will not update. So even though I have a cell for June, I do not need it until I get to July at which point I will need it.

The variable I am using to assign values to update the PT is CQV0i where i = the month # in the quarter. So since there are 2 months in the quarter, i = 2 and I need 202304 to be CQV01 and 202305 to be CQV02. Furthermore, once the variables have been assigned it needs to update the PT up to the # of months. The PT we use is very basic and it updates based on the values you need only. So for the 2 values I have my PT code would be:

With PT1
     ActiveSheet.PivotTables("BaseCQ").PivotFields( _
        "[Fiscal Period].[Fiscal Year - Month].[Month]").VisibleItemsList = Array( _
        "[Fiscal Period].[Fiscal Year - Month].[Month].&[1]&[" & CQV01 & "]", _
        "[Fiscal Period].[Fiscal Year - Month].[Month].&[1]&[" & CQV02 & "]", _
        "[Fiscal Period].[Fiscal Year - Month].[Month].&[1]&[" & CQV03 & "]")
End With

Each time the month # increases for the quarter, I need to add a line. So if we were in April, I would only need the first line / CQV0i = CQV01. May I would need two lines CQV01 and CQV02. June I would need all 3 CQB01, CQV02, amd CQV03. I cannot update the PT with a blank value, I get an error. So based which month # it is in the quarter, I only need the corresponding # of lines in the code.

This is what I have written so far.

Dim PT1 As PivotTable
Dim x As Long
Dim i As Range
Dim CQV0i As Long

x = 1
Set i = Range("CurrRAngeNum")
Set PT1 = Worksheets("FDW_BaseCY").PivotTables("FDW_BaseCY")

Do Until x = i
Set CQV0i = Range("CQTRVLE0" & i)
I need a line here to update CQV0i and assign each of the months

Pivot Table Code here from PT1 to PT7

x = x + 1
Loop

It wont let me use CQV0i (I get "Object Required"). I've been scouring Stack Online for different ways I can approach this and Frankenstein my code) but I've run up against it here. I know it's a lot and it's pretty complex (for me at least) but I would appreciate any help or guidance. I am going to keep refreshing this page if there are questions but I'm still going to be looking for answers so responses may be delayed. Thank you.

Havent been able to get VBA to acknowledge my dynamic variable. Keep getting Object Required.

答案1

得分: 1

我是天才

Sub DU()

Dim PT1 As PivotTable
Dim x As Long
Dim i As Range
Dim CQV0x As Long
Dim CQV01 As Variant
Dim CQV02 As Variant

x = 1
Set i = Range("CurrRAngeNum")
Set PT1 = Worksheets("BaseCY").PivotTables("BaseCY")

Do Until x > i

If i = 1 Then
CQV0x = Range("CQTRVLE0" & x).Value
CQV01 = CQV0x
    
ElseIf i = 2 Then
CQV0x = Range("CQTRVLE0" & x).Value
CQV01 = CQV0x
x = x + 1
CQV0x = Range("CQTRVLE0" & x).Value
CQV02 = CQV0x

ElseIf i = 3 Then
CQV0x = Range("CQTRVLE0" & x).Value
CQV01 = CQV0x
x = x + 1
CQV0x = Range("CQTRVLE0" & x).Value
CQV02 = CQV0x
x = x + 1
CQV0x = Range("CQTRVLE0" & x).Value
CQV03 = CQV0x
End If

x = x + 1
Loop

If i = 1 Then
   
    ActiveSheet.PivotTables("BaseCY").PivotFields( _
        "[Fiscal Period].[Fiscal Year - Month].[Month]").VisibleItemsList = Array( _
        "[Fiscal Period].[Fiscal Year - Month].[Month].&[" & CQV01 & "]")
    
ElseIf i = 2 Then
    
    ActiveSheet.PivotTables("BaseCY").PivotFields( _
        "[Fiscal Period].[Fiscal Year - Month].[Month]").VisibleItemsList = Array( _
        "[Fiscal Period].[Fiscal Year - Month].[Month].&[" & CQV01 & "]", _
        "[Fiscal Period].[Fiscal Year - Month].[Month].&[" & CQV02 & "]")
    
ElseIf i = 3 Then
    
    ActiveSheet.PivotTables("BaseCY").PivotFields( _
        "[Fiscal Period].[Fiscal Year - Month].[Month]").VisibleItemsList = Array( _
        "[Fiscal Period].[Fiscal Year - Month].[Month].&[" & CQV01 & "]", _
        "[Fiscal Period].[Fiscal Year - Month].[Month].&[" & CQV02 & "]", _
        "[Fiscal Period].[Fiscal Year - Month].[Month].&[" & CQV03 & "]")

End If

End Sub

得清理一下并添加一些PTs,但这个代码可用。希望有人会找到它有用。

英文:

I'm a genius

Sub DU()

Dim PT1 As PivotTable
Dim x As Long
Dim i As Range
Dim CQV0x As Long
Dim CQV01 As Variant
Dim CQV02 As Variant


x = 1
Set i = Range("CurrRAngeNum")
Set PT1 = Worksheets("BaseCY").PivotTables("BaseCY")

Do Until x > i

    If i = 1 Then
    CQV0x = Range("CQTRVLE0" & x).Value
    CQV01 = CQV0x
        
    ElseIf i = 2 Then
    CQV0x = Range("CQTRVLE0" & x).Value
    CQV01 = CQV0x
    x = x + 1
    CQV0x = Range("CQTRVLE0" & x).Value
    CQV02 = CQV0x
    
    ElseIf i = 3 Then
    CQV0x = Range("CQTRVLE0" & x).Value
    CQV01 = CQV0x
    x = x + 1
    CQV0x = Range("CQTRVLE0" & x).Value
    CQV02 = CQV0x
    x = x + 1
    CQV0x = Range("CQTRVLE0" & x).Value
    CQV03 = CQV0x
    End If
    
x = x + 1
Loop

    If i = 1 Then
       
        ActiveSheet.PivotTables("BaseCY").PivotFields( _
            "[Fiscal Period].[Fiscal Year - Month].[Month]").VisibleItemsList = Array( _
            "[Fiscal Period].[Fiscal Year - Month].[Month].&[1]&[" & CQV01 & "]")
        
    ElseIf i = 2 Then
        
        ActiveSheet.PivotTables("BaseCY").PivotFields( _
            "[Fiscal Period].[Fiscal Year - Month].[Month]").VisibleItemsList = Array( _
            "[Fiscal Period].[Fiscal Year - Month].[Month].&[1]&[" & CQV01 & "]", _
            "[Fiscal Period].[Fiscal Year - Month].[Month].&[1]&[" & CQV02 & "]")
        
    ElseIf i = 3 Then
        
        ActiveSheet.PivotTables("BaseCY").PivotFields( _
            "[Fiscal Period].[Fiscal Year - Month].[Month]").VisibleItemsList = Array( _
            "[Fiscal Period].[Fiscal Year - Month].[Month].&[1]&[" & CQV01 & "]", _
            "[Fiscal Period].[Fiscal Year - Month].[Month].&[1]&[" & CQV02 & "]", _
            "[Fiscal Period].[Fiscal Year - Month].[Month].&[1]&[" & CQV03 & "]")
    
    End If


End Sub

Gotta clean it up and add some more PTs but this works. Hope someone finds it useful.

huangapple
  • 本文由 发表于 2023年6月30日 03:17:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76584027.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定