Adding and Formatting a Shape via Macro

Written by Allen Wyatt (last updated November 11, 2023)
This tip applies to Excel 2007, 2010, 2013, and 2016


Ron often uses Shapes (an oval, actually) to highlight things in a worksheet. He adds and sizes the shape, then he needs to change the shape color to red and delete any fill color so that it doesn't obscure what he's actually trying to highlight. He performs these steps quite often, and has no doubt this could somehow be sped up with a macro, but he's not sure how to go about it.

Before looking at macro-based solutions, you might want to consider some that don't require macros. For instance, if you create your oval (formatted as desired) and then right-click on it, you can set it as your default shape. This can speed the addition of the shape to your workbook in the future.

If you have multiple shapes, each a different size and color, you may want to simply copy those shapes to a different workbook. That workbook could be a "holder" for those shapes, so that you can copy them back to other workbooks, as needed. This is a great way to build your own gallery of standard shapes that can be used for years.

If you decide that a macro is the best way to go, then it is relatively easy to use one to add the oval. Here's a very simple one:

Sub Custom_Oval1()
    ActiveSheet.Shapes.AddShape(msoShapeOval, 150, 150, _
      120, 60).Select
    Selection.ShapeRange.Fill.Visible = msoFalse
    With Selection.ShapeRange.Line
        .DashStyle = msoLineSolid
        .Weight = 3
        .Visible = msoTrue
        .ForeColor.RGB = RGB(255, 0, 0)
        .Transparency = 0
    End With
End Sub

This inserts a standard oval, with desired color, fill, and transparency, at a single location in the worksheet. When it is done, the shape is still selected and you can then drag the oval to where you desire and make any adjustments in size that may be necessary.

If you prefer to have the oval closer to where you want, you can use a variation that looks at the active cell in the worksheet and places the oval around that cell, sized to the height and width of the cell:

Sub Custom_Oval2()
    Dim lLeft As Long
    Dim lTop As Long
    Dim lWidth As Long
    Dim lHeight As Long

    lLeft = ActiveCell.Left - 5
    lWidth = ActiveCell.Width + 5
    lTop = ActiveCell.Top
    lHeight = ActiveCell.Height + 5

    ActiveSheet.Shapes.AddShape(msoShapeOval, lLeft, _
      lTop, lWidth, lHeight).Select
    Selection.ShapeRange.Fill.Visible = msoFalse
    With Selection.ShapeRange.Line
        .DashStyle = msoLineSolid
        .Weight = 3
        .Visible = msoTrue
        .ForeColor.RGB = RGB(255, 0, 0)
        .Transparency = 0
    End With
End Sub

Note that this variation of the macro determines the location for the shape based on the location of the ActiveCell object. You could, if desired, change all occurrences of ActiveCell to Selection, which would allow the size of the oval to adjust to surround your selection rather than simply the active cell. Note, as well, that the left, width, and height values are adjusted slightly to give the oval a better fit. You may want to play with different adjustment values to get just the look you want.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (4631) applies to Microsoft Excel 2007, 2010, 2013, and 2016.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Showing Text when a Cell is Empty

Wouldn't it be great if you could have Excel display some text in a cell only when that cell is empty? Unfortuantely, ...

Discover More

Calculating Fractions of Years

When working with dates and the relationship between dates, Excel provides a variety of worksheet functions that may ...

Discover More

Static Sizes for Comment Boxes

Adding comments to your worksheet can be helpful in documenting what the worksheet contains. If you want to make sure ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (ribbon)

Moving and Copying Graphics Objects

Excel doesn't just work with numbers and text. You can also add graphics objects to your worksheets, and then use Excel's ...

Discover More

Adding a Line Shape of a Given Slope and Length

If you want to create a line in your worksheet that is a specific length and slope, there are a couple of ways you can do ...

Discover More

Using WordArt in Excel

The WordArt program has been available in Office for a long, long time. It allows you to (as the name implies) create art ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 2 + 8?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.