New Excel Forum

This forum has been moved to TeachExcel.com

Ask all future questions in the New Excel Forum.

ExcelKey

Rotate Shapes On Your Worksheet

Free Excel Macros

Rotate Shapes On Your Worksheet

Postby Sisyphus » Fri Jun 28, 2013 11:10 pm

Any shape, such as an arrow, rectangle or triangle can be rotated by the number of degrees entered into a cell on the same worksheet. All it takes is a little code and a little understanding of the process. You will find both here.

There are two ways of rotating a shape, absolute or relative. An absolute direction always counts from 0, a relative one from the shape's current direction. In the attached workbook there are three worksheets, one each demonstrating absolute and relative rotation and the third one doing both. Each of these sheets has its code. You can copy/paste the code to the code sheet of your own project or you can copy/paste the entire sheet in which case Excel will include the code in the process. You must save the resulting workbook as macro-enabled (xlms extension) if you use Excel 2007 or later.

The code requires two adjustments to match the environment on your worksheet.
  1. Define the relevant cells
    At the top of the code you will find constants, like these.
    Const AbsAngle As String = "A1"
    Const RelAngle As String = "A2"
    Const AngleCell As String = "C14"
    You can specify the cells where the rotation angles will be entered. The last one is an output cell where the achieved absolute rotation angle will be indicated after a relative rotation was carried out. You can specify any cells on the worksheet. Only the Absolute or Relative rotation requires all three cells.
  2. Specify the shape to be rotated.
    In place of this code: Set Sh = Shapes("MyShape")
    you could use this: Set Sh = Shapes(1)
    All the shapes in each worksheet are numbered. Excel also gives them a name. If you have only one it may be most convenient just to use the second syntax - addressing it as "Shapes(1)"
In the code module ShapeMan you will find some useful code snippets to help you deal with naming your shapes. The procedures GetName and SetName let you read the name of any shape on your worksheet and change the name. You can use SetName to give a name of your choice to a shape (naming conventions disallow some special characters) and then use that name in addressing it in the rotation code.

Use the procedure ResetApplication if your code ever crashes and Excel becomes unresponsive therafter. Run any of these procedures by placing the cursor anywhere within it and press F5. GetName and SetName require you to select the shape you are interested in on the worksheet first. Copy the entire module into your project by dragging it in the Project Explorer window of the VB Editor.
You do not have the required permissions to view the files attached to this post.
Have a great day! :D

Sisyphus
I do this for "honour and country" - much less of the latter, actually.
If I helped you, award points, plenty of them.
If I bored you, deduct points for being too long-winded. (I know, :lol)
Sisyphus
Former Moderator
 
Posts: 4454
Joined: Dec 7, 2011
Location: Shanghai
Reputation: 203
Excel Version: 2010

Return to Excel Macros

Who is online

Users browsing this forum: No registered users and 32 guests