Excel Macros: Creating and managing drawing objects

crackjack

New Member
Joined
Jun 26, 2007
Messages
30
I am in dire need of help!

I need to create 4 drawing objects (let's say trapezium), group them into a specific pattern, and write inside them - the text inside each would be the value of a specific cell on a specific sheet. Let's say Sheet1!A1 Sheet1!A2 Sheet1!A3 and Sheet2!A1. And I need the text on the objects to change automatically to reflect the changes in the respective cells, failing which the user should be able to press a key combo (Ctrl Q, for example) to update the text on the objects. Please help, as I have no idea how to create or manage drawing objects in Excel using VBA.
 
You have not said what you are tring to do.

Here's what exactly I need to do:
1. Create 4 trapezoids names trap1, trap2, trap3, trap4 and height = 75, width = 200.
2. I need to specify the style for them - say number 37 for all ...in terms of texture/ look
3. I need to specify fillcolor for each. Say navy, aqua, yellow, maroon for trap1, trap2, trap3, trap4 respectively.
4. i need to turn them around (rotate).... trap2 by 270, trap3 by 180, trap4 by 90.
5. I need to adjust the curve so that the slants are 45 deg
6. I need to bring them together so that the small bases of all together form a square.
7. I need to attach textboxes to each, and pull in text for those textboxes from Sheet1 cells A1, A2, A3 and A4. It would be better still if the textboxes dynamically display the contents of those cells, but even copying would do.
8. I need to group them all together
 
Upvote 0
To record the macro need to have the Drawing Toolbar visible. Start the recorder. Select a shape from 'Autoshapes' and draw a box on the screen.

For finer formatting, set the macro recorder. Click a shape to get the surrounding outline & doubleclick on that. Always need to select a worksheet cell afterwards so the shape is not selected otherwise everything stops working.

There is a macro below called "SHOW_DETAILS" that puts the shape position data on to the worksheet for manual transfer to the macro. Use this after manual positioning.

Code:
'=======================================================================
'- CREATE & ARRANGE 4 TRAPEZOID SHAPES ON A WORKSHEET
'- (FROM DRAWING TOOLBAR - AUTOSHAPES - BASIC SHAPES)
'- Adds text from cells A1:A4
'- Brian Baulsom July 2007 - using Excel 2000
'=======================================================================
Dim TrapObj As Object           ' object to set properties
'- Trapezium Properties
'- to transfer values to the subroutine
Dim TrapTop As Double
Dim TrapLeft As Double
Dim TrapWidth As Double
Dim TrapHeight As Double
Dim TrapName As String
Dim TrapText As String
Dim TrapRotation As Integer
Dim TrapInterior As Integer
Dim TrapTextColor As Integer
'- Fill colours
Dim Navy As Integer
Dim Aqua As Integer
Dim Yellow As Integer
Dim Maroon As Integer
'-

'=======================================================================
'- MAIN ROUTINE
'=======================================================================
Sub ADD_SHAPES()
    Application.ScreenUpdating = False
    ActiveSheet.Shapes.SelectAll
    Selection.Delete
    '==============================
    Navy = 18
    Aqua = 49
    Yellow = 13
    Maroon = 61
    '-----------------------------------------------------------------
    '- TRAP 1
    TrapLeft = 118.5
    TrapTop = 104.25
    TrapWidth = 200.25
    TrapHeight = TrapWidth / 4      ' to get 45 degree angle
    TrapName = "Trap1"
    TrapText = ActiveSheet.Range("A1")
    TrapRotation = 0
    TrapInterior = Navy
    TrapTextColor = 2               ' white text
    MakeTrapezoid           ' SUBROUTINE
    '-----------------------------------------------------------------
    '- TRAP 2
    TrapLeft = 42.75
    TrapTop = 179.25
    TrapWidth = 200.25
    TrapHeight = TrapWidth / 4       ' to get 45 degree angle
    TrapName = "Trap2"
    TrapText = ActiveSheet.Range("A2")
    TrapRotation = 270
    TrapInterior = Aqua
    TrapTextColor = xlAutomatic
    MakeTrapezoid           ' SUBROUTINE
    '-----------------------------------------------------------------
    '- TRAP 3
    TrapLeft = 118.5
    TrapTop = 254.25
    TrapWidth = 200.25
    TrapHeight = TrapWidth / 4       ' to get 45 degree angle
    TrapName = "Trap3"
    TrapText = ActiveSheet.Range("A3")
    TrapRotation = 180
    TrapInterior = Yellow
    TrapTextColor = xlAutomatic
    MakeTrapezoid           ' SUBROUTINE
    '-----------------------------------------------------------------
    '- TRAP 4
    TrapLeft = 194.25
    TrapTop = 179.25
    TrapWidth = 200.25
    TrapHeight = TrapWidth / 4       ' to get 45 degree angle
    TrapName = "Trap4"
    TrapText = ActiveSheet.Range("A4")
    TrapRotation = 90
    TrapInterior = Maroon
    TrapTextColor = 2                ' white text
    MakeTrapezoid           ' SUBROUTINE
    '-----------------------------------------------------------------
    ActiveSheet.Range("A1").Select
    Application.ScreenUpdating = True
    MsgBox ("Done")
End Sub
'========= END OF MAIN ROUTINE =======================================

'=====================================================================
'- SUBROUTINE TO MAKE THE TRAPEZOID
'=====================================================================
Private Sub MakeTrapezoid()
    ' Type,Left,Top,Width,Height
    Set TrapObj = ActiveSheet.Shapes.AddShape _
        (msoShapeTrapezoid, TrapLeft, TrapTop, TrapWidth, TrapHeight)
    With TrapObj
        .Name = TrapName
        .Fill.ForeColor.SchemeColor = TrapInterior
        .Rotation = TrapRotation
        .Placement = xlFreeFloating
        With .TextFrame
            .Characters.Text = TrapText
            .Characters.Font.Size = 10
            .Characters.Font.ColorIndex = TrapTextColor
            .HorizontalAlignment = xlHAlignCenter
            .VerticalAlignment = xlVAlignCenter
       End With
    End With
End Sub
'======================================================================

'======================================================================
'- RUN THIS AFTER MANUAL ARRANGEMENT TO SHOW SHAPE POSITIONS
'======================================================================
Sub SHOW_DETAILS()
    ActiveSheet.Range("E1:I1").Value = Array("NAME", "LEFT", "TOP", "WIDTH", "HEIGHT")
    R = 2
    For Each s In ActiveSheet.Shapes
        Cells(R, 5).Value = s.Name
        Cells(R, 6).Value = s.Left
        Cells(R, 7).Value = s.Top
        Cells(R, 8).Value = s.Width
        Cells(R, 9).Value = s.Height
        R = R + 1
    Next
End Sub
'============================================================
 
Upvote 0
Thanks a bunch, Brian! This was very helpful.

Apparently in Excel 12/ 2007, the macro recorder does not record what you do with shapes :-( But now, thanks to you, I know how to work shapes.
 
Upvote 0

Forum statistics

Threads
1,226,796
Messages
6,193,048
Members
453,772
Latest member
aastupin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top