You are on page 1of 5

Dedicat ed t rainers

Maximum 6 per cIass


Superb courseware
Phone ( 01457) 858877 or emaiI
Search: Go
Home > Blogs
HOW TO CREATE AUTOSHAPES, LI NES AND CONNECTORS I N VBA MACROS
Par t six of an eight - part ser ies of bIogs
You can use Visual Basic wit hin Excel, PowerPoint or Word t o draw shapes, format
t hem and even assign macros t o run - t his long blog gives lot s of ideas of how t o
proceed!
1. Wor king wit h shapes in VBA
2. Wor king wit h shapes - get t ing st ar t ed
3. Naming, referr ing t o and posit ioning shapes
4. Format t ing shapes and adding t ext
5. Adding lines and connect ors t o j oin shapes t oget her
6. Wor king wit h mult iple shapes ( t his ar t icle)
7. Assigning macros t o shapes
8. Exercises on shapes
Post ed by Andy Br own on 25 Januar y 2014 | no comment s
Working wit h muIt ipIe shapes
There are t wo main ways t o work wit h a number of shapes at t he same t ime: by
using t he ShapeRange obj ect , or by looping over a collect ion of shapes.
Most Microsoft examples use t he first met hod ( t he ShapeRange
obj ect ) ; I find t he second much easier t o work wit h, however.
Examples of bot h are shown below.
Using t he ShapeRange obj ect
You can use an obj ect of t ype ShapeRange t o get access t o a set of shapes, allowing
you t o set propert ies and apply met hods t o a set of shapes simult aneously. Here' s an
example of it s use:
We want t o colour t hese cir cles
pink, and give t hem a red border .
Cont act us
RSS feed
BLOGS BY TOP C
General ( 15)
MS Office ( 133)
SQL Ser ver ( 85)
.NET ( 30)
BY AUTHOR
Andr ew Gould ( 78)
Andy Br own ( 156)
David Wakefield ( 3)
Jenny Brown ( 1)
Michael Allsop ( 12)
BY YEAR
2010 ( 2)
2011 ( 76)
2012 ( 100)
2013 ( 45)
2014 ( 27)
PRNCE2 Courses fr 329.
theknowledgeacademy.com/ Prince2
Foundation & Practitioner Training. Best Price Guarantee, Exams inc.
MS Office t r aining SQL Server t r aining .NET t r aining Training venues Ot her
Page1 of 5 Working with multiple shapes
6/21/2014 http://www.wiseowl.co.uk/blog/s394/multiple-shapes.htm
One way t o do t his is t o draw t he shapes, t hen select t hem:
Sub AddCi r cl es( )
Di m ws As Wor ksheet
Di m c1 As Shape
Di m c2 As Shape
Set ws = Act i veSheet
' add t wo ci r cl es
Set c1 = ws. Shapes. AddShape( msoShapeOval , 10, 10, 50, 50)
Set c2 = ws. Shapes. AddShape( msoShapeOval , 70, 10, 50, 50)
' now f or mat t hem bot h
ws. Shapes. Sel ect Al l
Now t hat t he shapes are select ed, we can apply t he ShapeRange met hod t o t he
current select ion t o ret urn a set of shapes ( did warn you t hat preferred t he ot her
met hod! ) :
' get a r ef er ence t o t hi s set of shapes
Di m sr As ShapeRange
Set sr = Sel ect i on. ShapeRange
' col our t hese shapes pi nk wi t h r ed bor der
sr . Fi l l . For eCol or . RGB = RGB( 250, 220, 240)
sr . Li ne. For eCol or . RGB = RGB( 250, 100, 150)
End Sub
You can abbreviat e t his as follows:
' now f or mat t hem bot h
ws. Shapes. Sel ect Al l
' col our t hese shapes pi nk wi t h r ed bor der
Sel ect i on. ShapeRange. Fi l l . For eCol or . RGB = RGB( 250, 220, 240)
Sel ect i on. ShapeRange. Li ne. For eCol or . RGB = RGB( 250, 100, 150)
The problem wit h t his approach is t hat it doesn' t allow for aut ocomplet ion:
Commands beginning wit h Select ion don' t support I nt ellisense.
Because of t he above limit at ion, it ' s easier t o creat e an int ermediat e
ShapeRange variable and use t his t o refer t o a set of shapes.
Looping over a coIIect ion of shapes
t ' s j ust so much easier ( humbly submit ) t o loop over all of t he shapes, colouring
each in t urn:
Page2 of 5 Working with multiple shapes
6/21/2014 http://www.wiseowl.co.uk/blog/s394/multiple-shapes.htm
Sub AddCi r cl es( )
Di m ws As Wor ksheet
Di m c1 As Shape
Di m c2 As Shape
Di m s As Shape
Set ws = Act i veSheet
' add t wo ci r cl es
Set c1 = ws. Shapes. AddShape( msoShapeOval , 10, 10, 50, 50)
Set c2 = ws. Shapes. AddShape( msoShapeOval , 70, 10, 50, 50)
' f or mat each of t hese shapes
For Each s n ws. Shapes
s. Fi l l . For eCol or . RGB = RGB( 250, 220, 240)
s. Li ne. For eCol or . RGB = RGB( 250, 100, 150)
Next s
End Sub
accept t hat t he above code probably runs more slowly, but you would have t o have
a serious number of shapes on a worksheet for t his t o mat t er!
Checking t he t ype of shapes
One useful t hing t o be able t o do is t o check what t ype each shape is as you loop over
it . You can do t his by t est ing a shape' s Type, and t hen more specifically it s
Aut oShapeType:
We' ll writ e code t o colour t he ovals,
but not t he r ect angle.
Here' s some code t o achieve t he above:
Sub For mat Onl yCi r cl es( )
Di m ws As Wor ksheet
Di m c1 As Shape
Di m c2 As Shape
Di m r 1 As Shape
Di m s As Shape
Set ws = Act i veSheet
' add t wo ci r cl es . . .
Set c1 = ws. Shapes. AddShape( msoShapeOval , 10, 10, 50, 50)
Set c2 = ws. Shapes. AddShape( msoShapeOval , 70, 10, 50, 50)
' . . . and a r ect angl e
Set r 1 = ws. Shapes. AddShape( msoShapeRect angl e, 10, 70, 110, 50)
' f or mat j ust t he ci r cl es
For Each s n ws. Shapes
' f i r st check f or aut oshapes
f s. Type = msoAut oShape Then
Page3 of 5 Working with multiple shapes
6/21/2014 http://www.wiseowl.co.uk/blog/s394/multiple-shapes.htm
' now check t hi s aut oshape i s a " ci r cl e"
f s. Aut oShapeType = msoShapeOval Then
s. Fi l l . For eCol or . RGB = RGB( 250, 220, 240)
s. Li ne. For eCol or . RGB = RGB( 250, 100, 150)
End f
End f
Next s
End Sub
Alt hough t his blog has concerned it self almost exclusively wit h aut oshapes, t here are
lot s of ot her shapes t hat you can add!
Just some of
t he ot her
shape t ypes
t hat you can
add t o a
workbook!
Having spent all of t his t ime looking at how t o creat e shapes, let ' s now t ake a quick
look at how t o assign macros t o t hem.
HOW TO CREATE AUTOSHAPES, LI NES AND CONNECTORS I N VBA MACROS
Par t six of an eight - part ser ies of bIogs
You can use Visual Basic wit hin Excel, PowerPoint or Word t o draw shapes, format
t hem and even assign macros t o run - t his long blog gives lot s of ideas of how t o
proceed!
1. Wor king wit h shapes in VBA
2. Wor king wit h shapes - get t ing st ar t ed
3. Naming, referr ing t o and posit ioning shapes
4. Format t ing shapes and adding t ext
5. Adding lines and connect ors t o j oin shapes t oget her
6. Wor king wit h mult iple shapes ( t his ar t icle)
7. Assigning macros t o shapes
8. Exercises on shapes
Comment s on t his bIog
This blog current ly has no comment s.
Add a comment t o t his blog
Recommend this on Google
Sign Up t o see what your friends like. Like Like
105 followers
Page4 of 5 Working with multiple shapes
6/21/2014 http://www.wiseowl.co.uk/blog/s394/multiple-shapes.htm
All cont ent copyright Wise Owl Business Solut ions Lt d 2014. All right s reserved.
Page5 of 5 Working with multiple shapes
6/21/2014 http://www.wiseowl.co.uk/blog/s394/multiple-shapes.htm

You might also like