Constructing A Shaperange Collection For Dynamically Generated Charts

  • Hi,


    I have a question about how to construct ShapeRange collection objects for dynamic shapes. I have a set of dynamically generated charts that I would like to group some of them together via VBA. This means that I need to construct a ShapeRange collection object dynamically. Unfortunately, the example provided by Microsoft only deals with static shapes, i.e., the number of shapes to be grouped and their names are known ahead of time. Below is the example from Microsoft:


    ----------------------------Microsoft Example--------------------------
    The following example constructs a shape range that contains the shapes named "Big Star" and "Little Star" on myDocument and applies a gradient fill to them.

    Code
    1. Set myDocument = Worksheets(1)
    2. Set myRange = myDocument.Shapes.Range(Array("Big Star", "Little Star"))
    3. myRange.Fill.PresetGradient _
    4. msoGradientHorizontal, 1, msoGradientBrass


    ------------------------------End Example-----------------------------


    The example above works for static shapes with predefined names. For dynamic shapes where their names are generated dynamically, one would think that replacing "Big Star", "Little Star" with a variable would do the trick, but it does not. For example the following code would generate an error message (shape not found):

    Code
    1. Dim strShapeNames as string
    2. Set myDocument = Worksheets(1)
    3. strShapeNames="""Big Star"", ""Little Star"""
    4. Set myRange = myDocument.Shapes.Range(Array(strShapeNames))


    Does this mean it is not possible to group dynamically generated charts? Has anyone tried to construct ShapeRange objects for dynamically generated charts?


    Thanks,


    Tom

  • Re: Constructing A Shaperange Collection For Dynamically Generated Charts


    Welcome to the forum. However please read the rules and use code tags for VBA. I have added them for you this time, but normally the post would be deleted.

  • Re: Constructing A Shaperange Collection For Dynamically Generated Charts


    Code
    1. Dim vntShapeName(1)
    2. Dim myDocument As Worksheet
    3. Dim myRange As ShapeRange
    4. Set myDocument = Worksheets(1)
    5. vntShapeName(0) = "Big Star"
    6. vntShapeName(1) = "Little Star"
    7. Set myRange = myDocument.Shapes.Range(vntShapeName)

    [h4]Cheers
    Andy
    [/h4]

    Edited once, last by Carim: Added Code Tags ().

  • Re: Constructing A Shaperange Collection For Dynamically Generated Charts


    Thanks Andy! Your code works.


    I have a follow up question. I noticed that the following code, although similar to yours, does not work:


    Code
    1. Dim vntShapeName as Variant
    2. Dim arrShapeName(1) as String
    3. Dim myDocument As Worksheet
    4. Dim myRange As ShapeRange
    5. Set myDocument = Worksheets(1)
    6. arrShapeName(0) = "Big Star"
    7. arrShapeName(1) = "Little Star"
    8. vntShapeName=arrShapeName
    9. Set myRange = myDocument.Shapes.Range(vntShapeName)


    According to Microsoft documentation, a variant of array and an array of variant are accessed the same way. So I am wondering why in this case one works and the other does not? Any idea?


    Thanks,


    Tom

  • Re: Constructing A Shaperange Collection For Dynamically Generated Charts


    I have no idea why one works and the other does not.

    [h4]Cheers
    Andy
    [/h4]

  • Sorry to relive such an old issue but wanted to explain why the code is invalid. In that code there are one implicit error that does not let debugging of shadow error.


    According to Microsoft documentation, the necessary argument for Range from the Shapes collection requires an array (of indices or text). Therefore, it must be explicitly declared as such, so Dim vntShapeName as Variant must become Dim vntShapeName() as Variant. https://docs.microsoft.com/en-…ba/api/Excel.Shapes.Range


    Now, if you execute debugging then VBE will info about of shadow error, you can not directly assign to matrix. This code is correct:


  • Hello,


    Hopefully, you have noticed this thread is ....14 years old ....:)

    If you feel like saying "Thank You" for the help received, do not hesitate to click the "Thumbs Up" icon, below, in the bottom right corner:)