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

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
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
the error says:
the specified parameter has an invalid value
runtime error 1004

thanks
 
Upvote 0

Forum statistics

Threads
1,213,531
Messages
6,114,172
Members
448,554
Latest member
Gleisner2

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