+ Reply to Thread
Results 1 to 2 of 2

Creating Slicers in Excel with VBScript (not VBA)

Hybrid View

Mrrrr Creating Slicers in Excel... 03-31-2023, 12:06 PM
Mrrrr Re: Creating Slicers in Excel... 03-31-2023, 06:37 PM
  1. #1
    Forum Contributor
    Join Date
    10-30-2019
    Location
    Bistrita, Romania
    MS-Off Ver
    2019; 365
    Posts
    231

    Creating Slicers in Excel with VBScript (not VBA)

    Hello,

    First I want to say that I am using VBS instead of VBA for multiple purposes:
    - I needed to make a script that works on multiple computers without having to either install an add-in, or add macros to the PERSONAL.XLSB file
    - I needed to make this work both with the SAP ERP and Excel (I can do it straight from VBA, see reason above why not)
    - I used VBS under HTA (HTML for Applications) to have a portable application that works
    - I needed a nice GUI to it, dynamic lists and checkboxes, that's why the HTA (I know most if not all of these can be made in UserForms)

    I've made a code that I'm not going to post in its entirety since it's not needed. Lemme describe what it does:
    - button 1: based on the info from the HTA GUI interface, I extract a table from SAP to Excel - no problem here
    - button 2: adds a Pivot table based on the data extracted by button 1, inserts Filters and some Calculated columns - no problem here

    Problem:
    - I'd like button 2 to also insert a couple of slicers to the Pivot table and can't seem to get my head around this

    What I did so far:
    - I recorded a macro inserting Slicers in Excel and tried to adapt it to VBS but couldn't
    - I am assuming I have to set a slicer cache but the syntax seems different than in Excel and all my tries either led to errors or to the code doing nothing

    Here's the part of the code I used to make the Pivot table and add some fields (and please see the attached sample file for data sheet + Pivot table):
    LR = .Cells(.Rows.Count, "D").End(xlUp).row
    LC = .Cells(1, .Columns.Count).End(xlLeft).column
    
    SrcData = "Sheet1!R1C1:R" & LR & "C" & LC
    
    Set pvtCache = wb.PivotCaches.Create(xlDatabase,SrcData,Version)
    Set pvtTable = pvtCache.CreatePivotTable(Destination,TableName)
    
    'rows
    pvtTable.pivotFields("Material").orientation = xlRowField
    
    'filters
    pvtTable.pivotFields("UL").orientation = xlFilterField
    pvtTable.pivotFields("Date").orientation = xlFilterField
    pvtTable.pivotFields("Unit").orientation = xlFilterField
    
    'etc
    'didn't post the calculated fields here since not needed
    Any help is appreciated, thank you for your time!
    Attached Files Attached Files
    Last edited by Mrrrr; 03-31-2023 at 06:38 PM.
    To show your appreciation
    Click ★ Add reputation!

  2. #2
    Forum Contributor
    Join Date
    10-30-2019
    Location
    Bistrita, Romania
    MS-Off Ver
    2019; 365
    Posts
    231

    Re: Creating Slicers in Excel with VBScript (not VBA)

    I finally found the answer myself. Maybe I had posted too little code above, so nobody could help.

    Anyway, here's the way you create Excel pivot table slicers from VBS:

    Dim slCache, SL, wsPvt
    Set wsPvt = wb.Worksheets("PIVOT")
    Set slCache = wb.SlicerCaches.Add2(pvtTable, "Unit")
    ' Syntax: Slicers.Add(SlicerDestination, Level, Name, Caption, Top, Left, Width, Height)
    Set SL = slCache.Slicers.Add(wsPvt, , "Unit", "Unit type")
    	SL.Top = 160
    	SL.Left = 490
    	SL.Width = 144
    	SL.Height = 188
    	SL.Style = "SlicerStyleDark1"

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 2
    Last Post: 04-29-2020, 04:49 PM
  2. Need help creating pivot tables and slicers off data
    By pigment01 in forum Excel General
    Replies: 1
    Last Post: 09-13-2018, 04:32 PM
  3. Replies: 0
    Last Post: 02-02-2018, 10:46 AM
  4. Connecting slicers from different data sets using table format (not by creating pivot)
    By karthikgmk in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-17-2016, 01:37 PM
  5. Connecting slicers from different data sets using table format (not by creating pivot)
    By karthikgmk in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-17-2016, 12:36 PM
  6. [SOLVED] Disconnect Slicers, Change Pivot Table Source, Reconnect Slicers: a problem
    By Geoff. in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-30-2015, 02:27 PM
  7. Using excel through vbscript
    By ashtom1 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-06-2005, 11:05 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1