VBA Group multiple shapes in each cell

jetiUser

New Member
Joined
Dec 17, 2015
Messages
6
Hi,

Need a little help to modify this code to do the following:

It needs to group all shapes that could be in each cell in column G (but only if it has more than one)
Each cell could have different shapes and number of shapes (a picture, ovals, arrows ..)

I tried to modify the original code from this thread to do so.


http://www.mrexcel.com/forum/excel-questions/505613-select-group-shapes-within-range-2.html

Option Explicit

Sub GroupShapesWithinCells()

Dim aShapes() As String
Dim oShape As Shape
Dim rRegion As Range
Dim Cnt As Long
Dim Rng As Range
Dim g As Range

For Each oShape In ActiveSheet.Shapes

If oShape.Type = msoGroup Then
oShape.Ungroup
End If

Next oShape

Set Rng = Range("G12", Range("G65536").End(xlUp))

For Each g In Rng

Cnt = 0
For Each oShape In ActiveSheet.Shapes

If Application.Union(g, Range(oShape.TopLeftCell, oShape.BottomRightCell)).Address = g.Address Then
Cnt = Cnt + 1
ReDim Preserve aShapes(1 To Cnt)
aShapes(Cnt) = oShape.Name
End If

Next oShape

If Cnt > 1 Then
ActiveSheet.Shapes.Range(aShapes).Group
End If

Next g

MsgBox "Done!"

End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Try :-
Code:
[COLOR=Navy]Sub[/COLOR] MG17Dec59
Dim Sp [COLOR=Navy]As[/COLOR] Shape
[COLOR=Navy]Dim[/COLOR] Ray()
[COLOR=Navy]Dim[/COLOR] p [COLOR=Navy]As[/COLOR] [COLOR=Navy]Integer[/COLOR]
p = 0
[COLOR=Navy]For[/COLOR] [COLOR=Navy]Each[/COLOR] Sp [COLOR=Navy]In[/COLOR] ActiveSheet.Shapes
    [COLOR=Navy]If[/COLOR] Sp.Type = msoAutoShape [COLOR=Navy]Then[/COLOR]
        [COLOR=Navy]If[/COLOR] Not Intersect(Range("G:G"), Sp.TopLeftCell) [COLOR=Navy]Is[/COLOR] Nothing [COLOR=Navy]Then[/COLOR]
            ReDim Preserve Ray(p)
            Ray(p) = Sp.Name
            p = p + 1
        [COLOR=Navy]End[/COLOR] If
    [COLOR=Navy]End[/COLOR] If
[COLOR=Navy]Next[/COLOR] Sp
[COLOR=Navy]If[/COLOR] p > 0 [COLOR=Navy]Then[/COLOR] ActiveSheet.Shapes.Range(Ray).Group
[COLOR=Navy]End[/COLOR] [COLOR=Navy]Sub[/COLOR]
Regards Mick
 
Upvote 0
So if you change the last bit of code as below and ensure the shapes are NOT grouped , does this return a Group Name
Code:
Dim Gr As Shape
If p > 1 Then
Set Gr = ActiveSheet.Shapes.Range(Ray).Group
MsgBox Gr.Name
End If
 
Upvote 0
No, it does not return any group names.

So if you change the last bit of code as below and ensure the shapes are NOT grouped , does this return a Group Name
Code:
Dim Gr As Shape
If p > 1 Then
Set Gr = ActiveSheet.Shapes.Range(Ray).Group
MsgBox Gr.Name
End If
 
Upvote 0
I've played and modified the code and sometimes it works, and sometimes it throws a "1004 error" on this line:

If Not Intersect(oShape.TopLeftCell, rRegion) Is Nothing Then


Code:
sub grouptest2()

Dim aShapes() As String
Dim oShape As Shape
Dim Rng As Range
Dim rRegion As Range
Dim g As Range
Dim Cnt As Long

For Each oShape In ActiveSheet.Shapes
    If oShape.Type = msoGroup Then
        oShape.Ungroup
    End If
Next oShape

Set Rng = Range("B12", Range("B65536").End(xlUp))
For Each g In Rng
    Set rRegion = g.Offset(0, 5)
    Cnt = 0
    For Each oShape In ActiveSheet.Shapes
        If Not Intersect(oShape.TopLeftCell, rRegion) Is Nothing Then
            Cnt = Cnt + 1
            ReDim Preserve aShapes(1 To Cnt)
            aShapes(Cnt) = oShape.Name
        End If
    Next oShape
    If Cnt > 1 Then
        ActiveSheet.Shapes.Range(aShapes).Group
    End If
    Set rRegion = Nothing
    Next g
MsgBox "Done"

End Sub
 
Upvote 0
Yes, as far as I can tell.
Usually there are only one picture (inserted) and one other shape (oval, circle or arrow) in each cell.

**Ran the following code to delete all the shapes (just to make sure). Deletes everything.

Sub DeleteShapes()
Dim oShape As Shape
For Each oShape In ActiveSheet.Shapes
oShape.Delete
Next Shp
End Sub

Are you shapes "Autoshapes", See VbHelp for definition.
 
Upvote 0
The following does what I want, most of the time. I can run the same code on the same sheet with no issues x number of times. After it fails on a "1004 application-defined or object-defined" error on this line, then it wont run anymore. Until I close the workbook and it will run again x number of times.

If Not Application.Intersect(oShape.TopLeftCell, rRegion) Is Nothing Then


Code:
Sub GroupShapes2()

Dim aShapes() As String
Dim oShape As Shape
Dim Rng As Range
Dim rRegion As Range
Dim g As Range
Dim Cnt As Long

For Each oShape In ActiveSheet.Shapes

    If oShape.Type = msoGroup Then
        oShape.Ungroup
    End If

Next oShape
Set Rng = Range("B12", Range("B65536").End(xlUp))
    
For Each g In Rng
    Set rRegion = g.Offset(0, 5)
    Cnt = 0

    For Each oShape In ActiveSheet.Shapes
        If Not Application.Intersect(oShape.TopLeftCell, rRegion) Is Nothing Then
            Cnt = Cnt + 1
            ReDim Preserve aShapes(1 To Cnt)
            aShapes(Cnt) = oShape.Name
        End If
        
    Next oShape

    If Cnt > 1 Then
        ActiveSheet.Shapes.Range(aShapes).Group
    End If

    Set rRegion = Nothing
    Next g

MsgBox "Done"

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,375
Messages
6,119,167
Members
448,870
Latest member
max_pedreira

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top