0
votes

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
3
So in G57 you have a comma-separated list of quoted values? You can't create an array from that by passing it into Array() Maybe try instead something like Split(myStr, ",")Tim Williams
Any chance you can post a screenshot of what's in G57 ? It's not clear from your question what that looks like.Tim Williams
@Tim Williams: Best I can do instead of screenshot is value string: "[Date].[Month].&[2014-02-01T00:00:00]" , "[Date].[Month].&[2014-01-01T00:00:00]"...AaronK
See my updated answer - I think if the array required by VisibleSlicerItemsList is zero-based then that should do it.Tim Williams
@TimWilliams: I do appreciate the help and suspect we're close, but I'm still getting a 1004 error when setting the VisibleSlicerItemList = myArrayAaronK

3 Answers

1
votes

Try this (based on values in G43:G55)

Sub Test()
Dim wb As Workbook
Dim shtF as Worksheet
Dim myArray
Dim str2 As String

    Set wb = ThisWorkbook
    Set shtF = wb.Worksheets("Filters")

    str2 = shtF.Range("D32").Value
    myArray = Get1DArray( shtF.Range("G43:G55") )

    With wb.SlicerCaches("Slicer_Month1")
        If str2 = "All" Then
            .ClearManualFilter
        Else
            .VisibleSlicerItemsList = myArray
        End If
    End With

End Sub

'get a 1-D zero-based array of values from a single-row/column
'   range of cells
Function Get1DArray(rng As Range)
    Dim arr
    arr = Application.Transpose(rng.Value)
    If rng.Rows.Count = 1 Then arr = Application.Transpose(arr)
    'convert from 1-based to zero-based array
    Get1DArray = Split(Join(arr, vbNull), vbNull)
End Function
0
votes

Edited below. Tim is correct. Try this:

    Sub Test()
    Dim wb As Workbook
    Dim myStr As Variant
    Dim str2 As String

    Set wb = ThisWorkbook
    myStr = Split(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 = myStr
    End If

    End Sub
0
votes

Edit1:

If indeed all your Filters are in G57 as what Tim pointed out, you can also try below :)

Sub AnotherPossibleWay()

Dim str1 As String, str2 As String
Dim myStr As Variant
Dim wb As Workbook

Set wb = Thisworkbook

str1 = Replace(wb.Worksheets("Filters").Range("G57").Value, ",", """,""")
str1 = """" & str1 & """"
myStr = Evaluate("{" & str1 & "}")
str2 = wb.Worksheets("Filters").Range("D32").Value

If str2 = "All" Then
    wb.SlicerCaches("Slicer_Month1").ClearManualFilter
Else
    wb.SlicerCaches("Slicer_Month1").VisibleSlicerItemsList = myStr
End If

End Sub

Hope this doesn't confuse you in a way.
The main thing is, convert your Range Value to Array correctly.