Passing a string variable to Excel Slicer.selected() errors

Hi All;
I'm using VB6 and want to set the value in a slicer on an .xls sheet.
If I use this code:
'first, set Slicer_Date1 to today's date (or passed in date)
    Dim sC As SlicerCache
    Set sC = ActiveWorkbook.SlicerCaches("Slicer_Date1")
    sC.SlicerItems("7/5/2013").Selected = True

It runs as expected.

If I try to replace the "7/5/2013" with a variable however, I get "Run Time error 5 - invalid procedure call or argument" on line 5 below.

code:
1    Dim sDateToUse As String
2    sDateToUse = Format(Now, "m/d/yyyy")
3    Dim sC As SlicerCache
4    Set sC = ActiveWorkbook.SlicerCaches("Slicer_Date1")
5    sC.SlicerItems(sDateToUse).Selected = True

I've tried:
    putting another set of double quotes around sDateToUse
    putting brackets around sDateToUse (found that out on the web somewhere)
    passing sDateToUse in as a date
    forcing sDateToUse = "7/5/2013" to be sure it wasn't a data issue

None of the above work.
Can some one help me ID what I'm missing?
Thanks so much!
k heitzsoftware developerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
Hi,

Is the date format in your slicer different from the one in the Pivot table

then change the format to the slicer one

Regards
0
Rgonzo1971Commented:
Could you send a dummy?
0
k heitzsoftware developerAuthor Commented:
Hi Rgonzo ~
The original file is an MS Access field defined as short date.
I made these changes to the variable (see lines 4 and 7)

1  Dim sDateToUse As String
2  Dim dtToUse As Date
3  sDateToUse = Format(Now, "m/d/yyyy")
4  dtToUse = CDate(sDateToUse)
5  Dim sC As SlicerCache
6  Set sC = ActiveWorkbook.SlicerCaches("Slicer_Date1")
7  sC.SlicerItems(dtToUse).Selected = True

but still get the same error.

What kind of a dummy are you looking for?
If the xls sheet - It's an .xlsb and its very large and attaches to many d/bs and csvs - all proprietary. I can ask the author to generate a dummy though. Can you specify what you'd like on it?

Thanks!
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Rgonzo1971Commented:
Just before line 5 pls insert

Debug.Print sC.SlicerItems(1).Name
Debug.Print sC.SlicerItems(2).Name
Debug.Print sC.SlicerItems(sC.SlicerItems.Count).Name

and tell me what you see in the immediate window when you run the code
0
k heitzsoftware developerAuthor Commented:
Hi Rgonzo ~
I figured it out! I was trying to use a date that was not in the pivot table because I was setting the date before refreshing the data.
When I refresh first and use an available date this code works:
sDateToUse = "6/29/2015"
Dim sC As SlicerCache
Set sC = ActiveWorkbook.SlicerCaches("Slicer_Date1")
sC.SlicerItems(sDateToUse).Selected = True

I split the points.
Thanks for your help!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
k heitzsoftware developerAuthor Commented:
If there is no matching data in the pivot table the Error 5 will be thrown.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.