• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

VBA to select first item in a slicer causes Invalid procedure call or argument|VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / VBA to select first item in a slicer causes Invalid procedure call or argument|VBA & Macros|Excel Forum|My Online Training Hub

vba course banner

Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumVBA & MacrosVBA to select first item in a slice…
sp_PrintTopic sp_TopicIcon
VBA to select first item in a slicer causes Invalid procedure call or argument
Avatar
Shawn Wallack
Member
Members
Level 0
Forum Posts: 77
Member Since:
December 7, 2021
sp_UserOfflineSmall Offline
1
May 13, 2022 - 5:18 am
sp_Permalink sp_Print sp_EditHistory

Hello -

I have a macro called "ResetReport" that deletes all slicers on a sheet and then adds back (and formats) four specific slicers to the same sheet. This is essentially a mechanism to "reset" a report to the way it was originally setup. The last step in the procedure connects the new slicers to the table that they will control. Everything up to and including that point works fine.

What I'm trying to do next is de-select all slicer items except the first so that the chart does not show an overwhelming amount of information (i.e., all options from all slicers). The code I've written (some borrowed, some original) causes an error.

Sub ResetReport

  ' The example I'm putting here simplifies things for purposes of this question

  'Call DeleteAllSlicers ' Remove all existing slicers
  'Call AddDefaultSlicers ' Add 2-4 slicers, depending on the active sheet from which this sub is called
  
  ' Note that the slicers created in this routine are given the following names
  ' Slicer #1: ActiveSheet.Name & Chr(32) & "ART"
  ' Slicer #2: ActiveSheet.Name & Chr(32) & "TEAM"
  ' Slicer #3: ActiveSheet.Name & Chr(32) & "PI"
  ' Slicer #4: ActiveSheet.Name & Chr(32) & "SPRINT"

 'Call SetSlicerProperties ' Formats the new slicers (header, caption, position, size, etc.)
 'Call ConnectSlicersToTable ' Connects the slicers to the table on the sheet
 
  Dim strSlicerName As String
  strSlicerName = ActiveSheet.Name & Chr(32) & "ART" ' I'm hard coding this here so you can see what's passed
  Call SliceByIndex(strSlicerName) ' This is the sub that's erroring out

End Sub

 

Sub SliceByIndex(strSlicerName As String)
Debug.Print Chr(10)
Debug.Print "START SliceByIndex (" & strSlicerName & ")" ' This line prints, so we get this far

  ' Declare variables for this procedure
  Dim sc As SlicerCache
  Dim si As SlicerItem
  Dim lSlicerItemsCount As Long
  Dim n As Long

  MsgBox "The name of the slicer is: " & strSlicerName ' This shows the correct slicer name 

  ' Set variable values
  Set sc = ThisWorkbook.SlicerCaches(strSlicerName) ' This line triggers Err 5: "Invalid procedure call or argument"

  ' Count slicer items
  lSlicerItemsCount = sc.SlicerItems.Count ' We never get this far
  Debug.Print "Number of slicer items: " & lSlicerItemsCount

  With sc

  ' Temporarily suspend PivotCache recalculation
  Debug.Print "Suspend PivotCache recalculation..."
  .PivotTables(1).ManualUpdate = True

  ' Select first slicer item (required)
  Debug.Print "Select first slicer item..."
  .SlicerItems(1).Selected = True

  ' Deselect all otehr slicer items
  For n = 2 To lSlicerItemsCount
  Debug.Print "Deselect slicer item: " & n & "..."
  .SlicerItems(n).Selected = False
  Next n

  ' Resume PivotCache recalculation
  Debug.Print "Resume PivotCache recalculation..."
  .PivotTables(1).ManualUpdate = False

End With

' Skip over error handler
GoTo GOODBYE

ERROR_HANDLER:

' Display error
Msgbox ("SliceByIndex", Err.Number, Err.Description, strSlicerName)

GOODBYE:

debug.print "END: SliceByIndex"
End Sub

sp_AnswersTopicSeeAnswer See Answer
Avatar
Velouria
London or thereabouts
Moderator
Members


Trusted Members

Moderators
Level 4
Forum Posts: 649
Member Since:
November 1, 2018
sp_UserOfflineSmall Offline
2
May 13, 2022 - 10:36 pm
sp_Permalink sp_Print

That error means that there isn't a slicer cache with the name specified. Usually they start with "Slicer_" by default

Avatar
Shawn Wallack
Member
Members
Level 0
Forum Posts: 77
Member Since:
December 7, 2021
sp_UserOfflineSmall Offline
3
May 14, 2022 - 3:17 am
sp_Permalink sp_Print

Thanks, as always. I'll continue to investigate, but...

The message box toward the top of the SliceByIndex sub displays this message: "The name of the slicer is: ReportVelocity ART." And when I look at the slicer, I see this:

Slicer-Settings.JPGImage Enlarger

So, I gather there is a difference between the "Name" property (which I'm using, and which is causing the error) and the "Name to use in formulas" property (which you're suggesting I need to use instead). 

If so, then how can I retrieve the "Name to use in formulas" property for a slicer that I just created using VBA? How do I know whether it's Slicer_ART1 or Slicer_2 or Slicer_ART3 or whatever?

sp_PlupAttachments Attachments
  • sp_PlupImage Slicer-Settings.JPG (36 KB)
Avatar
Velouria
London or thereabouts
Moderator
Members


Trusted Members

Moderators
Level 4
Forum Posts: 649
Member Since:
November 1, 2018
sp_UserOfflineSmall Offline
4
May 16, 2022 - 5:44 pm
sp_Permalink sp_Print

More importantly, there is a difference between a SlicerCache and a Slicer.

The SlicerCache name is "Slicer_" and the name of the Source field (I don't know if that prefix varies depending on language). Since you are creating the slicers in your code, you can refer to the SlicerCache property of each one directly to manipulate that.

sp_AnswersTopicAnswer
Answers Post
Avatar
Shawn Wallack
Member
Members
Level 0
Forum Posts: 77
Member Since:
December 7, 2021
sp_UserOfflineSmall Offline
5
May 17, 2022 - 6:16 am
sp_Permalink sp_Print

Thank you, Velouria. Everything is working now!

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Orlando Menes, Alicia Greynolds, Lee Mapes
Guest(s) 2
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 873
Purfleet: 414
Frans Visser: 346
David_Ng: 306
lea cohen: 222
Jessica Stewart: 218
A.Maurizio: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
Blair Gallagher
Brandi Taylor
Hafiz Ihsan Qadir
Gontran Bage
adolfo casanova
Annestine Johnpulle
Priscila Campbell
Jeff Mikles
Aaron Butler
Maurice Petterlin
Forum Stats:
Groups: 3
Forums: 24
Topics: 6369
Posts: 27852

 

Member Stats:
Guest Posters: 49
Members: 32359
Moderators: 3
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: MOTH Support, Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.