BLOGS BY TOPIC
BLOGS BY AUTHOR
BLOGS BY YEAR
You can use Visual Basic within Excel, PowerPoint or Word to draw shapes, format them and even assign macros to run - this long blog gives lots of ideas of how to proceed!
- Working with shapes in VBA (this blog)
- Working with shapes - getting started
- Naming, referring to and positioning shapes
- Formatting shapes and adding text
- Adding lines and connectors to join shapes together
- Working with multiple shapes
- Assigning macros to shapes
- Exercises on shapes
Posted by Andy Brown on 25 January 2014
You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.
Working with shapes in VBA
For a long time I've always shied away from this subject, but it turns out that I shouldn't have: shapes in VBA are surprisingly easy to create and control!
The code to create these two shapes is shown in the formatting section of this blog.
This blog goes into the minutiae of how to create autoshapes in VBA. All of the examples are for Excel, but they'll work just as well for PowerPoint VBA or Word VBA.
First, however, there are two things that you can do to make your shape-life easier (it sounds like something out of a bad science fiction novel ...).
Tip 1: Use Excel 2010 or later
Admittedly you may not have much choice about this, but if you possibly can, use Excel 2010 in preference to Excel 2007. Imagine that you want to add a rectangle while recording a macro. Here's what this produces when recording a macro in Excel 2010:
' Macro3 Macro
ActiveSheet.Shapes.AddShape(msoShapeRectangle, 355.0588188976, 64.2352755906, _
Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = "test"
With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(1, 4). _
.FirstLineIndent = 0
.Alignment = msoAlignLeft
With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(1, 4).Font
.NameComplexScript = "+mn-cs"
.NameFarEast = "+mn-ea"
.Fill.Visible = msoTrue
.Fill.ForeColor.ObjectThemeColor = msoThemeColorLight1
.Fill.ForeColor.TintAndShade = 0
.Fill.ForeColor.Brightness = 0
.Fill.Transparency = 0
.Size = 11
.Name = "+mn-lt"
Here's exactly the same thing recorded in Excel 2007:
' Macro1 Macro
You can easily survive using shapes without recording anything, but being able to record commands can make life much easier!
Tip 2: Use a Worksheet variable
For some reason, VBA doesn't support autocompletion that well when you're working with worksheets. For example, as you type in the following lines of code you won't see any Intellisense to help you:
'neither of the following autocompletes
Worksheets(1).Shapes.AddShape 1, 1, 1, 1, 1
ActiveSheet.Shapes.AddShape 1, 1, 1, 1, 1
Websites will tell you that this is because Excel doesn't know whether you're working with a worksheet or a chart, but I can't see how this can be true for the first line of code above. Whatever ...
By contrast, if you use an object variable to refer to a worksheet life will be MUCH easier:
If you use a variable of type Worksheet, VBA knows what you're talking about and can help you.
With those two tips out of the way, let's start with the basics of working with shapes!