+ Reply to Thread
Results 1 to 11 of 11

VBA to select value in slicer based on shape name

  1. #1
    Registered User
    Join Date
    09-17-2023
    Location
    Norway
    MS-Off Ver
    Excel 2016
    Posts
    6

    VBA to select value in slicer based on shape name

    Hi
    In the attached workbook, I have made an easy excample of what I'm trying to solve.

    I have a front page with shapes that will control the slicers in the other page. So if I press "USA", or "CA", it will choose that shape's name (or caption) in the slicer/filter value.

    The table is in the datamodel, and only as an "connection".

    I believe the country shapes need one macro, and the state-shapes a different macro, since it is two different slicers.

    I have tried so many VBA versions, without luck.

    Can someone try to make this work, thanks?
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,223

    Re: VBA to select value in slicer based on shape name

    Assign sample macros to the corresponding shapes.
    Please Login or Register  to view this content.
    Artik

  3. #3
    Registered User
    Join Date
    09-17-2023
    Location
    Norway
    MS-Off Ver
    Excel 2016
    Posts
    6

    Re: VBA to select value in slicer based on shape name

    Thanks for your suggestion. Unfortunately, your solution requires me to create a unique macro for each of the shapes. In the file I'm going to use, I will have a lot of shapes like countries, so I need a vba that gets the name of the shape that calls the macro (alternatively, gets the text in the shape that calls the macro). Then all the "country"-shapes can have the same vba.

    Remember this attached file is only a simple example.

  4. #4
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,223

    Re: VBA to select value in slicer based on shape name

    I don't see any major problems. For the "State" shapes, create slightly different names so that there is a link between State and Country.
    We assign one macro to all shapes
    Please Login or Register  to view this content.
    Artik
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    09-17-2023
    Location
    Norway
    MS-Off Ver
    Excel 2016
    Posts
    6

    Re: VBA to select value in slicer based on shape name

    Yeah, that works! Thank you for your quick and precise reply.

    Appreciate if you would answer the following questions as well:
    1. How does the ubound work in this vba?
    2. If I would add the next level "store_city" under state. Which solution would you recommend then?

    Thanks again

  6. #6
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,223

    Re: VBA to select value in slicer based on shape name

    Quote Originally Posted by eivstei View Post
    1. How does the ubound work in this vba?
    Application.Caller returns the name of the clicked object.
    Split(s, "-") creates an array by splitting the name into elements. In this case, the separator is a hyphen. The array formed by splitting the string is always indexed from 0.
    UBound(v) returns the upper index of the array (LBound - the lower index).
    In the following conditions, I check what the upper index is. If 0, it means that basically no division has taken place (there is no separator), which would indicate that it is the highest level - Country. If Ubound returns 1 it means that we are dealing with State.


    Quote Originally Posted by eivstei View Post
    2. If I would add the next level "store_city" under state. Which solution would you recommend then?
    On a similar basis as before. You create an object name "with an access path", e.g. "USA-CO-Denver". You add another condition. It would probably look like this
    Please Login or Register  to view this content.
    Artik

  7. #7
    Registered User
    Join Date
    09-17-2023
    Location
    Norway
    MS-Off Ver
    Excel 2016
    Posts
    6

    Re: VBA to select value in slicer based on shape name

    Thanks Artik!

  8. #8
    Registered User
    Join Date
    09-17-2023
    Location
    Norway
    MS-Off Ver
    Excel 2016
    Posts
    6

    Re: VBA to select value in slicer based on shape name

    Hi again Artik!
    I tried your last code with several levels (country-state-city-..), and it works perfectly.

    I turn to you again hoping that you can help me with one more thing. I have attached the latest version of file.

    I want a headline in the Dashboard sheet (textbox, label or similar).
    I need it to contain the same value as choosen in the slicer.
    Since I have three slicers in this case, it need to be the last changed value in one of the three slicers.

    As an Alternative B solution if the other is difficult, I can have a textbox for each slicer.
    Attached Files Attached Files

  9. #9
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,223

    Re: VBA to select value in slicer based on shape name

    No matter which shape you click, its name is always the last element of the "path". v(Ubound(v)) will return that last element to you.
    Change the code snippet according to the following:
    Please Login or Register  to view this content.
    Artik

  10. #10
    Registered User
    Join Date
    09-17-2023
    Location
    Norway
    MS-Off Ver
    Excel 2016
    Posts
    6

    Re: VBA to select value in slicer based on shape name

    Yes, that works nice when user click the shapes. I forgot to mention that the users also can click directly in the slicers, and therfore I need a code that everytime a change in one of the slicers, the last choosen value should appear in the textbox. Is that possible?

  11. #11
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,223

    Re: VBA to select value in slicer based on shape name

    I suggest starting a new thread because what you are asking significantly goes beyond the topic of the current thread.
    In general, it will be about creating a responsive slicer event that.... doesn't exist. But smart people have come up with something. Take a look at Jan Karel Pieterse.

    Artik

+ 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. Select Slicer Value based on range of cell values
    By avmax in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-19-2023, 07:37 AM
  2. Select Pivot/Slicer Value based on Cell Input
    By ptmuldoon in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-28-2022, 10:30 AM
  3. Replies: 0
    Last Post: 10-01-2020, 05:40 AM
  4. Macro to Select Shape Based on Filter
    By saravanan1981 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-15-2019, 04:28 AM
  5. A Slicer Is A Shape, Why Cant I Add A Macro
    By goss in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-31-2015, 10:45 AM
  6. [SOLVED] A macro after setting onaction for a shape that will select the shape.
    By vonRobbo in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-20-2014, 11:34 PM
  7. Select a shape based on a cell value and update the shape when value changes
    By BigAl99 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-14-2013, 12:06 PM

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