动态滚动X个月-数据透视过滤器多项选择

时间:2018-10-05 14:50:55

标签: vba excel-vba pivot-table

您好,我继承了使用Microsoft Analysis Server数据源的仪表板。

为了提高性能,我希望用户选择一个日期范围,例如 上个月 最近3个月 最近6个月

我的想法是插入3个“选项”按钮,当用户选择按钮时,VBA代码将更改过滤器选择。

我记录了选择月份的操作,但需要有关如何进行操作的建议。

Sub SelectMonth()
'
' SelectMonth Macro
'

'
ActiveSheet.PivotTables("pv_DateRange").PivotFields( _
    "[Date].[Month - Year].[Month - Year]").VisibleItemsList = Array( _
    "[Date].[Month - Year].&[201711]", "[Date].[Month - Year].&[201712]", _
    "[Date].[Month - Year].&[201801]", "[Date].[Month - Year].&[201802]", _
    "[Date].[Month - Year].&[201803]", "[Date].[Month - Year].&[201804]", _
    "[Date].[Month - Year].&[201805]", "[Date].[Month - Year].&[201806]", _
    "[Date].[Month - Year].&[201807]", "[Date].[Month - Year].&[201808]", _
    "[Date].[Month - Year].&[201809]")
End Sub 
  1. 在上面的示例中,我选择的是选择最近12个月。我的数据中没有这种情况(2017年10月),因此没有显示选择2017年10月的选项。如何查看显示内容?或忽略是否尝试设置过滤器列表中未显示的值。

  2. 在我的VBA过滤器选择中,每月计算和设置的一种好方法是什么?

谢谢 史蒂夫

1 个答案:

答案 0 :(得分:0)

-未设置-

假设您选择的过滤器将采用“ yyyymm”或年月并置的格式。

我没有任何OLAP数据透视表,但是我使用的日期过滤是VBA中的其他一些数据透视表,所以我希望它可以工作,但尚未经过测试。

如果您要使用3个选项按钮并且只有最后1个月,最近3个月或最近6个月,则可以将这3个按钮作为ActiveXObject Command Buttons插入到电子表格中。

在VBA项目浏览器中,导航到出现按钮的那些工作表,您将看到CommandButton1_Click(),CommandButton2_Click()和CommandButton3_Click()的子例程。对于私人订阅,请将此代码放在命令按钮的位置 潜艇是:

Private Sub CommandButton1_Click()
SelectMonth (1)
End Sub

Private Sub CommandButton2_Click()
SelectMonth (3)
End Sub

Private Sub CommandButton3_Click()
SelectMonth (6)
End Sub

然后将您录制的宏替换为:

    Sub SelectMonth(NumOfMonths As Integer)
'
' Select Month Macro
'
'
' Declares string variables for the last 6 months relative to today
Dim Past1Month, Past2Month, Past3Month, Past4Month, Past5Month, Past6Month As String
Past1Month = Format(DateAdd("m", -1, Date), "yyyymm")
Past2Month = Format(DateAdd("m", -2, Date), "yyyymm")
Past3Month = Format(DateAdd("m", -3, Date), "yyyymm")
Past4Month = Format(DateAdd("m", -4, Date), "yyyymm")
Past5Month = Format(DateAdd("m", -5, Date), "yyyymm")
Past6Month = Format(DateAdd("m", -6, Date), "yyyymm")

'determines which button is pressed and filters pivot to desired selection
If NumOfMonths = 1 Then
    ActiveSheet.PivotTables("pv_DateRange").PivotFields( _
        "[Date].[Month - Year].[Month - Year]").VisibleItemsList = Array( _
        "[Date].[Month - Year].&[" & Past1Month & "]")
ElseIf numofmonth = 3 Then
    ActiveSheet.PivotTables("pv_DateRange").PivotFields( _
        "[Date].[Month - Year].[Month - Year]").VisibleItemsList = Array( _
        "[Date].[Month - Year].&[" & Past1Month & "]", _
        "[Date].[Month - Year].&[" & Past2Month & "]", _
        "[Date].[Month - Year].&[" & Past3Month & "]")
ElseIf numofmonth = 6 Then
    ActiveSheet.PivotTables("pv_DateRange").PivotFields( _
        "[Date].[Month - Year].[Month - Year]").VisibleItemsList = Array( _
        "[Date].[Month - Year].&[" & Past1Month & "]", _
        "[Date].[Month - Year].&[" & Past2Month & "]", _
        "[Date].[Month - Year].&[" & Past3Month & "]", _
        "[Date].[Month - Year].&[" & Past4Month & "]", _
        "[Date].[Month - Year].&[" & Past5Month & "]", _
        "[Date].[Month - Year].&[" & Past6Month & "]")
End If

End Sub

另一个选项是动态选择,因此用户可以输入任何月份。您只需要1个命令按钮,其代码为:

    Private Sub CommandButton4_Click()

Dim x As Variant
x = InputBox("Enter Desired Number of Months to View", "Last X Months", 1)

If Not IsNumeric(x) Or x < 1 Then
    MsgBox "Please enter a valid positive number.", vbCritical, "ERROR"
    Exit Sub
End If

x = CInt(x)

SelectMonthDynamic (x)
End Sub

然后在模块中放置以下代码:

    Sub SelectMonthDynamic(NumOfMonths As Integer)
'
' User can enter any month
'
Dim DateArray() As Variant
Dim i As Integer
Dim PastXMonths As String

'creates an array that is the same size as your user's selection in months
ReDim DateArray(1 To NumOfMonths) As Variant

'adds each month to the array in descending order
For i = NumOfMonths To 1 Step -1
    DateArray(i) = "[Date].[Month - Year].&[" & Format(DateAdd("m", -i, Date), "yyyymm") & "]"
Next i

'concatenates the values in the array with a comma separator
PastXMonths = Join(DateArray, ",")

'filters the pivot to that selection
'   NOT SURE IF THIS WORKS
ActiveSheet.PivotTables("pv_DateRange").PivotFields( _
    "[Date].[Month - Year].[Month - Year]").VisibleItemsList = Array(PastXMonths)

End Sub
相关问题