Consulting

Results 1 to 6 of 6

Thread: Refer to Grouped Charts (or anything that's grouped) in VBA

  1. #1

    Question Refer to Grouped Charts (or anything that's grouped) in VBA

    Hello all,

    I'd just like to quickly ask how one can refer to grouped objects, both in general and for charts in particular, in VBA?


    I have the following for referring to single charts:

    Sub Picture1_Click()
        ActiveSheet.ChartObjects("Chart 6").Visible = True
        ActiveSheet.ChartObjects("Chart 9").Visible = True
        ActiveSheet.ChartObjects("Chart 7").Visible = False
        ActiveSheet.ChartObjects("Chart 8").Visible = False
    End Sub
    Private Sub CommandButton2_Click()
        ActiveSheet.ChartObjects("Chart 6").Visible = False
        ActiveSheet.ChartObjects("Chart 9").Visible = False
        ActiveSheet.ChartObjects("Chart 7").Visible = True
        ActiveSheet.ChartObjects("Chart 8").Visible = True
    End Sub
    Now this obviously looks tedious but grouping them should help make it more easier to manage. But I can't seem to find the appropriate function for a group of charts anywhere online. I have tried a few combinations including ChartGroups (with the Group name in the brackets) and GroupItems, but each time I keep getting the following error message:

    "Object doesn't support this property or method"


    Any help would be appreciated. Thanks!

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    After you've grouped a bunch of charts on the sheet, leave the group selected and see what the group's name is in the top left of the screen in the Names field (to the left of the formula bar where it normally shows the address of the active cell). You can also find the name in the Immediate pane of the VBE if, while the group is still selected, you type ?selection.name and press Enter.
    Let's say it turns out to be Group 8, then you can use it in VBA thus:
      ActiveSheet.Shapes("Group 8").Visible = msoFalse '/msoTrue
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    Quote Originally Posted by p45cal View Post
    After you've grouped a bunch of charts on the sheet, leave the group selected and see what the group's name is in the top left of the screen in the Names field (to the left of the formula bar where it normally shows the address of the active cell). You can also find the name in the Immediate pane of the VBE if, while the group is still selected, you type ?selection.name and press Enter.
    Let's say it turns out to be Group 8, then you can use it in VBA thus:
      ActiveSheet.Shapes("Group 8").Visible = msoFalse '/msoTrue
    Thank you for the reply p45cal!


    I tried your solution and it works perfectly.



    One question though. I noticed you put Visible = msoFalse instead of simply Visible = False. Is there a reason for doing so? I swapped the mso out to see what would happen, but it turns out the macro works just as well. Just curious.

  4. #4
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Quote Originally Posted by noname91 View Post
    I noticed you put Visible = msoFalse instead of simply Visible = False. Is there a reason for doing so?
    I'm sure it's the same. I just used what I saw against the .Visible property in the Locals pane of the VBE.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    FWIW --

    The class MsoTristate has members:

    msoCTrue = 1 (not supported)
    msoFalse = 0
    msoTristateMixed = -2 (not supported)
    msoTristateToggle = -3 (not supported)
    msoTristateTrue = -1


    The class vbTristate has members

    vbFalse = 0
    vbTrue = -1
    vbUseDefault = -2



    Regular ol' VBA has

    False = 0
    True = -1

    So I figured some MS Office programmer had time on his hands and decided to complicate things with the mso's , although vbUseDefault is used in some format routines

    Since the enumerations translate back to a Long, it most probably won't make a difference
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  6. #6
    Ah I see. Thanks for confirming p45cal and Paul for providing that extra bit of info!

Posting Permissions

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