I'm trying to set slicer values based on code to select a range of 13 consecutive months. If I use the variable in the code, it creates a type mismatch error. If I debug.print the variable and paste the values instead of the variable, the code works just fine. Am I missing something when trying to use the variable myStr?
Code that Errors out:
Sub Test()
Dim wb As Workbook
Dim myStr As Variant
Dim str2 As String
Set wb = ThisWorkbook
myStr = wb.Worksheets("Filters").Range("G57").Value
str2 = wb.Worksheets("filters").Range("D32").Value
If str2 = "All" Then
wb.SlicerCaches("Slicer_Month1").ClearManualFilter
Else
wb.SlicerCaches("Slicer_Month1").VisibleSlicerItemsList = Array(myStr)
End If
End Sub
Code that works (Values in lieu of myStr are from a ?mystr :
Sub Test()
Dim wb As Workbook
Dim myStr As Variant
Dim str2 As String
Set wb = ThisWorkbook
'myStr = wb.Worksheets("Filters").Range("G57").Value
str2 = wb.Worksheets("filters").Range("D32").Value
If str2 = "All" Then
wb.SlicerCaches("Slicer_Month1").ClearManualFilter
Else
wb.SlicerCaches("Slicer_Month1").VisibleSlicerItemsList = Array("[Date].[Month].& _
[2014-02-01T00:00:00]", "[Date].[Month].&[2014-01-01T00:00:00]", "[Date].[Month].& _
[2013-12-01T00:00:00]", "[Date].[Month].&[2013-11-01T00:00:00]", "[Date].[Month].& _
[2013-10-01T00:00:00]", "[Date].[Month].&[2013-09-01T00:00:00]", "[Date].[Month].& _
[2013-08-01T00:00:00]", "[Date].[Month].&[2013-07-01T00:00:00]", "[Date].[Month].& _
[2013-06-01T00:00:00]", "[Date].[Month].&[2013-05-01T00:00:00]", "[Date].[Month].& _
[2013-04-01T00:00:00]", "[Date].[Month].&[2013-03-01T00:00:00]", "[Date].[Month].& _
[2013-02-01T00:00:00]")
End If
End Sub
EDIT:
My original code had a Join(Transpose(Range),",") (see below) to create the array instead of using a consolidated string in cell G57. I concatenated the results into G57 to basically create the desired string in a troubleshooting effort.
mystr = _
Join(Application.WorksheetFunction.Transpose(Worksheets("Filters").Range("G43:G55")),",")
.VisibleSlicerItemList = Array(myStr)
I can't post a screenshot since I do not have enough points yet, but cells G43:G55 contain the correct time/date stamp required to set the slicer values. The format is "[Date].[Month].&[YYYY-MM-DD&"T"&HH:MM:SS]". The values in G57 are concatenated results from G43:G55 separated by commas and includes quotes.
EDIT 2:
It's not a real solution and is completely inelegant, but time is money so I cheated and declared variables for each of the 13 possible date values as a temporary solution. Part of the reporting package shows monthly values based on a selected month and the 12 months prior, so at least the number of possible values is fixed. It doesn't really solve the problem for others who are in need of a similar solution -- imagine 100+ possible selections, cringe!
Private Sub Worksheet_Calculate()
Dim wb As Workbook
Dim str2 As String
Dim mstr1 As Variant
Dim mstr2 As Variant
Dim mstr3 As Variant
Dim mstr4 As Variant
Dim mstr5 As Variant
Dim mstr6 As Variant
Dim mstr7 As Variant
Dim mstr8 As Variant
Dim mstr9 As Variant
Dim mstr10 As Variant
Dim mstr11 As Variant
Dim mstr12 As Variant
Dim mstr13 As Variant
'Other code runs here
Set wb = ThisWorkbook
mstr1 = wb.Worksheets("Filters").Range("G43").Value
mstr2 = wb.Worksheets("Filters").Range("G44").Value
mstr3 = wb.Worksheets("Filters").Range("G45").Value
mstr4 = wb.Worksheets("Filters").Range("G46").Value
mstr5 = wb.Worksheets("Filters").Range("G47").Value
mstr6 = wb.Worksheets("Filters").Range("G48").Value
mstr7 = wb.Worksheets("Filters").Range("G49").Value
mstr8 = wb.Worksheets("Filters").Range("G50").Value
mstr9 = wb.Worksheets("Filters").Range("G51").Value
mstr10 = wb.Worksheets("Filters").Range("G52").Value
mstr11 = wb.Worksheets("Filters").Range("G53").Value
mstr12 = wb.Worksheets("Filters").Range("G54").Value
mstr13 = wb.Worksheets("Filters").Range("G55").Value
str2 = wb.Worksheets("filters").Range("D32").Value
If str2 = "All" Then
wb.SlicerCaches("Slicer_Month1").ClearManualFilter
Else
wb.SlicerCaches("Slicer_Month1").VisibleSlicerItemsList = Array(mstr1, mstr2, mstr3, mstr4, mstr5, mstr6, mstr7, mstr8, mstr9, mstr10, mstr11, mstr12, mstr13)
End If
'More code runs here
ErrHandler:
RestoreXL
Exit Sub
End Sub
Array()
Maybe try instead something likeSplit(myStr, ",")
– Tim WilliamsVisibleSlicerItemsList
is zero-based then that should do it. – Tim WilliamsVisibleSlicerItemList = myArray
– AaronK