Hi - bit of background.
I'm trying to set up an OLAP cube via VBA to update 2 tables - one is for the current month, and the other is for all history UP THROUGH last month. For example, if the selection is March 2015, I want one to pull everything in March, and the other to pull everything through February. The code works fine to update the current month, but since there are no multiple selections, it works fine. Something in generating the multiple selection string is causing the issue.
Warning: there are multiple named ranges in a worksheet to populate these variables.
The issue is here: .PivotFields("[Status].[Status Date - Hierarchy].[Status Date - Year]").VisibleItemsList = Array(aryYear)
The error is Run-time 1004 - Application-defined or object-defined error.
Not sure how to fix this. Help is appreciated.
I'm trying to set up an OLAP cube via VBA to update 2 tables - one is for the current month, and the other is for all history UP THROUGH last month. For example, if the selection is March 2015, I want one to pull everything in March, and the other to pull everything through February. The code works fine to update the current month, but since there are no multiple selections, it works fine. Something in generating the multiple selection string is causing the issue.
Warning: there are multiple named ranges in a worksheet to populate these variables.
Code:
Sub UpdateStatusMonth()
Dim i As Integer
Dim rngYear As String
Dim rngQtr As String
Dim rngMonth As String
Dim rngLastMonth As String
Dim rngYearStart As String
Dim intLastMonth As String
Dim intLastMonthEnd As String
Dim intLastMonthStart As String
Dim aryYear As String
Dim aryQtr As String
Dim aryMonth As String
Dim aryTest As String
rngYear = Application.VLookup(Range("rngCubeFilterYear"), Range("rngCubeFilter"), 4, False)
rngQtr = Application.VLookup(Range("rngCubeFilterMonth"), Range("rngCubeFilter"), 3, False)
rngMonth = Range("rngCubeFilterMonth")
aryTest = "[Status].[Status Date - Hierarchy].[Status Date - Mon].&[" & rngYear & "]&[" & rngQtr & "]&[" & rngMonth & "]"
With Worksheets("StatusCurrent").PivotTables("pvtStatus")
.PivotFields("[Status].[Status Date - Hierarchy].[Status Date - Year]").VisibleItemsList = Array("")
.PivotFields("[Status].[Status Date - Hierarchy].[Status Date - Qtr]").VisibleItemsList = Array("")
.PivotFields("[Status].[Status Date - Hierarchy].[Status Date - Day]").VisibleItemsList = Array("")
.PivotFields("[Status].[Status Date - Hierarchy].[Status Date - Mon]").VisibleItemsList = _
Array(aryTest)
'"[Status].[Status Date - Hierarchy].[Status Date - Mon].&[" & rngYear & "]&[" & rngQtr & "]&[" & rngMonth & "]"
End With
Worksheets("Annotation").Range("R1").Value = aryTest
rngYear = Application.VLookup(Range("rngCubeFilterYear"), Range("rngCubeFilter"), 4, False)
rngQtr = Application.VLookup(Range("rngCubeFilterMonth"), Range("rngCubeFilter"), 3, False)
rngMonth = Application.VLookup(Range("rngCubeFilterMonth"), Range("rngCubeFilter"), 2, False)
rngLastMonth = rngYear & rngQtr & rngMonth
intLastMonthEnd = Application.Index(Range("rngMonthID"), Application.Match(rngLastMonth, Range("rngMonthIDLong"), 0)) - 1
'Create Year array
rngYear = ""
rngYear = Application.VLookup(intLastMonthEnd / 1, Range("rngMonthIndex"), 2, False)
For i = 0 To rngYear - 2012 - 1
If aryYear = "" Then
aryYear = "[Status].[Status Date - Hierarchy].[Status Date - Year].&[" & 2012 + i & "]"
Else
aryYear = aryYear & ", " & "[Status].[Status Date - Hierarchy].[Status Date - Year].&[" & 2012 + i & "]"
End If
Next i
'Create Month array - also fills quarter
intLastMonthStart = Application.Index(Range("rngMonthID"), Application.Match(rngYear / 1, Range("rngFullYear"), 0))
aryMonth = ""
For i = intLastMonthStart To intLastMonthEnd
If Range("rngCubeFilterMonth").Value = 1 Then GoTo Skip_i
rngMonth = Application.VLookup(i, Range("rngMonthIndex"), 4, False)
rngQtr = Application.VLookup(rngMonth / 1, Range("rngMonthIndex"), 3, False)
If aryMonth = "" Then
aryMonth = "[Status].[Status Date - Hierarchy].[Status Date - Mon].&[" & rngYear & "]&[" & rngQtr & "]&[" & rngMonth & "]"
Else
aryMonth = aryMonth & ", " & "[Status].[Status Date - Hierarchy].[Status Date - Mon].&[" & rngYear & "]&[" & rngQtr & "]&[" & rngMonth & "]"
End If
'End If
Next i
Skip_i:
Worksheets("Annotation").Range("R4").Value = aryYear
Worksheets("Annotation").Range("R3").Value = aryMonth
With Worksheets("StatusHistory").PivotTables("pvtStatus")
[B][COLOR=#ff0000] .PivotFields("[Status].[Status Date - Hierarchy].[Status Date - Year]").VisibleItemsList = _[/COLOR][/B]
[B][COLOR=#ff0000] Array(aryYear)[/COLOR][/B]
.PivotFields("[Status].[Status Date - Hierarchy].[Status Date - Qtr]").VisibleItemsList = _
Array("")
If aryMonth = "" Then
.PivotFields("[Status].[Status Date - Hierarchy].[Status Date - Mon]").VisibleItemsList _
= Array("")
Else: .PivotFields("[Status].[Status Date - Hierarchy].[Status Date - Mon]").VisibleItemsList _
= Array(aryMonth)
End If
.PivotFields("[Status].[Status Date - Hierarchy].[Status Date - Day]").VisibleItemsList = Array("")
End With
End Sub
The issue is here: .PivotFields("[Status].[Status Date - Hierarchy].[Status Date - Year]").VisibleItemsList = Array(aryYear)
The error is Run-time 1004 - Application-defined or object-defined error.
Not sure how to fix this. Help is appreciated.