Applying same error check to multiple small macros. My code is too repetitive.

melpa

New Member
Joined
May 19, 2016
Messages
10
On a worksheet I have multiple shapes and several buttons attached to simple macros which manipulate the shapes: eg rotate, flip etc.

Also attached to each button is a procedure to handle the error that occurs if a shape has not first been selected. (I will show the code below.)

The problem I want to address is the repetitiveness of my code: Every simple macro is imbedded in the same lengthy error-checking code. How can I approach this in a more concise way?

Code:
 Sub Rotate90 ()
Dim ActiveShape As Shape
Dim UserSelection As Variant

Set UserSelection = ActiveWindow.Selection

On Error GoTo NoShapeSelected
Set ActiveShape = ActiveSheet.Shapes(UserSelection.Name)
On Error Resume Next

Selection.ShapeRange.IncrementRotation 90

Exit Sub

Error handler
NoShapeSelected:
MsgBox "You do not have a shape selected "

End Sub

Currently I am writing this whole thing around every simple one line instruction.

Thanks in advance.

(I got this code from The Spreadsheet Guru website)
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Try something like this...

Code:
[color=darkblue]Sub[/color] Rotate90()
    [color=darkblue]If[/color] IsShapeSelected [color=darkblue]Then[/color]
        Selection.ShapeRange.IncrementRotation 90
    [color=darkblue]End[/color] [color=darkblue]If[/color]
[color=darkblue]End[/color] [color=darkblue]Sub[/color]


[color=darkblue]Function[/color] IsShapeSelected() [color=darkblue]As[/color] [color=darkblue]Boolean[/color]
    [color=darkblue]Dim[/color] ActiveShape [color=darkblue]As[/color] Shape
    [color=darkblue]Dim[/color] UserSelection [color=darkblue]As[/color] [color=darkblue]Variant[/color]
    
    [color=darkblue]Set[/color] UserSelection = ActiveWindow.Selection
    
    [color=darkblue]On[/color] [color=darkblue]Error[/color] [color=darkblue]GoTo[/color] NoShapeSelected
    [color=darkblue]Set[/color] ActiveShape = ActiveSheet.Shapes(UserSelection.Name)
    [color=darkblue]On[/color] [color=darkblue]Error[/color] [color=darkblue]Resume[/color] [color=darkblue]Next[/color]
    
    IsShapeSelected = [color=darkblue]True[/color]
    
    [color=darkblue]Exit[/color] [color=darkblue]Function[/color]
    
    [color=green]'Error handler[/color]
NoShapeSelected:
    MsgBox "You do not have a shape selected "
    IsShapeSelected = [color=darkblue]False[/color]
End [color=darkblue]Function[/color]
 
Upvote 0
Thank you, AlphaFrog.
That was so quick!
And it's exactly what I needed.
Thanks for furthering my VBA education.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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