560 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owls only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
How to create autoshapes, lines and connectors in VBA macros
Part one of an eight-part series of blogs
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!
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 ...).
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!
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!
|Parts of this blog|
25 Aytoun Street