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!

  1. Working with shapes in VBA (this blog)
  2. Working with shapes - getting started
  3. Naming, referring to and positioning shapes
  4. Formatting shapes and adding text
  5. Adding lines and connectors to join shapes together
  6. Working with multiple shapes
  7. Assigning macros to shapes
  8. Exercises on shapes

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.

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!

Example of shapes

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:

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!

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:

Intellisense appearing

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! 

 

This blog has 0 threads Add post