Grouping command button

jasman

Board Regular
Joined
Oct 30, 2010
Messages
141
Hi. I have some images which I have written code to group them when a command button is clicked. but it also groups the command button with the images- anyway around this? Hers my code?

Thanks


Private Sub CommandButton2_Click()
Sheets("Sheet2").Select
ActiveSheet.Shapes.SelectAll
Selection.ShapeRange.GROUP.Select
End Sub
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
To exclude OLE control objects, such as a command button, try...

Code:
[font=Verdana][color=darkblue]Private[/color] [color=darkblue]Sub[/color] CommandButton2_Click()

    [color=darkblue]Dim[/color] MyArray() [color=darkblue]As[/color] [color=darkblue]String[/color]
    [color=darkblue]Dim[/color] Shp [color=darkblue]As[/color] Shape
    [color=darkblue]Dim[/color] Cnt [color=darkblue]As[/color] [color=darkblue]Long[/color]
    
    [color=darkblue]For[/color] [color=darkblue]Each[/color] Shp [color=darkblue]In[/color] Sheets("Sheet2").Shapes
        [color=darkblue]If[/color] Shp.Type <> 12 [color=darkblue]Then[/color]
            [color=darkblue]ReDim[/color] [color=darkblue]Preserve[/color] MyArray(Cnt)
            MyArray(Cnt) = Shp.Name
            Cnt = Cnt + 1
        [color=darkblue]End[/color] [color=darkblue]If[/color]
    [color=darkblue]Next[/color] Shp
    
    Sheets("Sheet2").Shapes.Range(MyArray).Group.Select

[color=darkblue]End[/color] [color=darkblue]Sub[/color]
[/font]

Actually, since the code resides within the worksheet module, try replacing...

Code:
Sheets("Sheet2").Shapes

with

Code:
Me.Shapes

and

Code:
Sheets("Sheet2").Shapes.Range(MyArray).Group.Select

with

Code:
Me.Shapes.Range(MyArray).Group.Select

This way you won't need to modify the code if the sheet name changes.
 
Last edited:
Upvote 0
thanks but it keeps showing an error on line

Sheets("Sheet2").Shapes.Range(MyArray).Group.Select

says parameter error
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,726
Members
452,939
Latest member
WCrawford

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