+ Reply to Thread
Results 1 to 4 of 4

Group shapes in a single cell without selecting them

  1. #1
    Forum Contributor
    Join Date
    03-07-2006
    Location
    India
    MS-Off Ver
    MSOffice 365 Professional 64 bit
    Posts
    108

    Group shapes in a single cell without selecting them

    I have multiple charts and some arrow shapes (linking to certain cells on Sheet2 to show the cell's value), all placed together inside the boundaries of a single merged cell E5.

    this code works when i add different shapes and even charts to the cell and then try to run the code to group them together. However i assume it doesn't seem to work for charts which reference ranges from a pivot...i may be wrong here, and it could be totally something.

    PHP Code: 
    Option Explicit

    Sub doit
    ()
        
    Call GroupShapes(Sheet2.Cells(5"E"))
    End Sub

    Sub GroupShapes
    (rngChart As Range)
        
    Dim Shp As Shape
        Dim ShpRng 
    As ShapeRange
        Dim ShpGrp 
    As Variant
        Dim Arr
    () As Variant
        Dim i 
    As Long

        i 
    1
        With rngChart
    .Parent
            
    For Each Shp In .Shapes
                
    If Shp.TopLeftCell.MergeArea.Row rngChart.MergeArea.Row Then
                    ReDim Preserve Arr
    (1 To i)
                    
    Arr(i) = Shp.Name
                    i 
    1
                End 
    If
            
    Next Shp

            Set ShpRng 
    = .Shapes.Range(Arr)

           
    //Here i get -> 'Application defined or Object defined error'
           
    Set ShpGrp ShpRng.Group

           With ShpGrp
              
    .Name "shp" VBA.Replace(rngChart.Parent.Name" """)
           
    End With
       End With
    End Sub 
    If i select all the shapes and then try to group them manually or via code (as shown below), it does Group. What am i doing wrong?

    PHP Code: 
    Sub doit1()
    Dim rngChart As Range

    Sheet2
    .Activate
    With Sheet2
        Set rngChart 
    = .Cells(5"E")
    End With
    Call GroupShapes1
    (rngChart)

    End Sub

    Sub GroupShapes1
    (rngChart As Range)
    Dim Shp As Variant
    Dim Arr
    () As Variant

    With rngChart
    .Parent
        
    For Each Shp In .Shapes
            
    If Not Intersect(.Range(Shp.TopLeftCell.MergeArea.CellsShp.BottomRightCell.MergeArea.Cells), rngChartIs Nothing Then
                Shp
    .Select Replace:=False
            End 
    If
        
    Next Shp
        Set Shp 
    Selection.Group
    End With
    End Sub 
    I want to group the shapes without selecting them. If anyone has any idea why this is happening, kindly assist.
    Attached Files Attached Files
    Last edited by junoon; 04-29-2016 at 11:52 AM. Reason: forgot to attach sample file

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,482

    Re: Group shapes in a single cell without selecting them

    Start by giving each shape a unique name.
    Cheers
    Andy
    www.andypope.info

  3. #3
    Forum Contributor
    Join Date
    03-07-2006
    Location
    India
    MS-Off Ver
    MSOffice 365 Professional 64 bit
    Posts
    108

    Re: Group shapes in a single cell without selecting them

    Tried that, doesn't work!
    Another observation is, If you see, the Arr() shows all the names that fall in the cell, but if you do a ShpRng.Select, the ShpRng only selects some shapes, excluding the charts. I think somehow there is a reason for this behavior.

  4. #4
    Forum Contributor
    Join Date
    03-07-2006
    Location
    India
    MS-Off Ver
    MSOffice 365 Professional 64 bit
    Posts
    108
    I found that the reason the charts were not getting selected was because each chart was named the same I.e. Object13, though their ID's were different. So once I renamed each shape and chart with a unique name ( here ID would just suffice), the grouping was possible.

    PHP Code: 
    Option Explicit 

    Sub doit
    () 
        
    Call GroupShapes(Sheet2.Cells(5"E")) 
    End Sub 

    Sub GroupShapes
    (rngChart As Range
    Dim Shp As Shape 
    Dim ShpRng 
    As ShapeRange 
    Dim ShpGrp 
    As Variant 
    Dim Arr
    () As Variant 
    Dim i 
    As Long i 

    With rngChart
    .Parent 
        
    For Each Shp In .Shapes 
            
    If Shp.TopLeftCell.MergeArea.Row rngChart.MergeArea.Row Then 
                With Shp
                    
    .Name = .Type & .ID
                End With

                ReDim Preserve Arr
    (1 To i
                
    Arr(i) = Shp.Name 
                i 

            End 
    If 
        
    Next Shp 

        Set ShpRng 
    = .Shapes.Range(Arr
        
    Set ShpGrp ShpRng.Group 

        With ShpGrp 
            
    .Name "shp" VBA.Replace(rngChart.Parent.Name" """
        
    End With 
    End With 
    End Sub 
    Last edited by junoon; 05-02-2016 at 09:46 AM.

+ 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. [SOLVED] Selecting a Single Cell in Commandbutton
    By trevor2524 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-03-2014, 11:51 AM
  2. [SOLVED] Group Shapes
    By zplugger in forum Excel General
    Replies: 3
    Last Post: 04-03-2013, 11:12 AM
  3. Replies: 2
    Last Post: 03-19-2013, 04:31 PM
  4. [SOLVED] Group Shapes with VBA
    By nicotob in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-17-2012, 05:12 AM
  5. Replies: 6
    Last Post: 07-10-2012, 11:30 AM
  6. Selecting a Range not just a single cell
    By Sanecrazy in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 03-07-2012, 09:31 AM
  7. Group and Name Shapes - How
    By barryleajo in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-04-2010, 07:32 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