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
Search our website
We also send out useful tips in a monthly email newsletter ...
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!
This is one small part of our free online Excel VBA tutorial. To find out how to learn in a more structured way, have a look at our training courses 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 ...).
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:
Sub Macro3()
'
' Macro3 Macro
'
'
ActiveSheet.Shapes.AddShape(msoShapeRectangle, 355.0588188976, 64.2352755906, _
62.1176377953, 48.7059055118).Select
Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = "test"
With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(1, 4). _
ParagraphFormat
.FirstLineIndent = 0
.Alignment = msoAlignLeft
End With
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
.Fill.Solid
.Size = 11
.Name = "+mn-lt"
End With
Range("I14").Select
End Sub
Here's exactly the same thing recorded in Excel 2007:
Sub Macro1()
'
' Macro1 Macro
'
'
Range("D10").Select
End Sub
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!
Although our two-day advanced VBA course (online or classroom) doesn't usually get onto working with shapes (it's a bit TOO niche), you will learn lots of other advanced VBA techniques. Meet the owls behind the blogs, and treat yourself to a place on one of our VBA courses!
Parts of this blog |
---|
Some other pages relevant to the above blogs include:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2023. All Rights Reserved.