Solved

I want to write a macro for a shape in Excel

Posted on 2007-11-16
9
1,930 Views
Last Modified: 2011-09-20
I have this circle (shape) that i inserted in excel and I want to write a macro that changes the inside color of this shape. I tried recording while i did it and that didn't work, i tried to record me inserting a shape and that didn't work either. Any ideas..... Is this possible
0
Comment
Question by:tchristie33
9 Comments
 
LVL 10

Expert Comment

by:János Szabó
ID: 20301468
ActiveSheet.Shapes.AddShape(msoShapeOval, X0, Y0, 40, 40).Select
'to get the index of the last inserted shape
ShapesCount = ActiveSheet.Shapes.Count
'to color the circle
ActiveSheet.Shapes(ShapesCount).Fill.ForeColor.RGB = RGB(255,0,0)

I hope this help

wbr Janos
0
 
LVL 38

Expert Comment

by:jeverist
ID: 20301473
Hi tchristie33,

You can check for the circle by type and or name and set the Interior.Color property like this:

Sub ChangeShapeColor()
Dim shp As Shape

For Each shp In ActiveSheet.Shapes
    If shp.Type = msoAutoShape Then
        If shp.Name = "Oval 1" Then
            shp.DrawingObject.Interior.Color = vbRed ' Red
        End If
    End If
Next shp

End Sub

Jim
0
 
LVL 10

Expert Comment

by:János Szabó
ID: 20301680
If you place the
for x=0 to 255
for y=o to 255
for z=0 to 255
    RGB(x,y,z)
    Do
        If Application.Wait(Now + TimeValue("0:00:1")) Then Exit Do 'wait for 1 sec
    Loop
next
next
next
you can change the wait time according your needs
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 33

Expert Comment

by:Jeroen Rosink
ID: 20304986
Kacor,
you madea typo in your code, instead of using 0 you used o.
for y=o to 255


0
 
LVL 10

Expert Comment

by:János Szabó
ID: 20305135
roos01,

you have right, sorry.I wrote it into comment field. But this is tested

Sub ShapeColorChange()
Dim X As Integer, Y As Integer, Z As Integer, ShapesCount As Integer
Dim X0 As Integer, Y0 As Integer
    'Exit on Esc
    X0 = 100
    Y0 = 400
    ActiveSheet.Shapes.AddShape(msoShapeOval, X0, Y0, 40, 40).Select
    'to get the index of the last inserted shape
    ShapesCount = ActiveSheet.Shapes.Count
    'to color the circle
    For X = 0 To 255 Step 16
        For Y = 0 To 255 Step 16
            For Z = 0 To 255 Step 16
                ActiveSheet.Shapes(ShapesCount).Fill.ForeColor.RGB = RGB(X, Y, Z)
                Do
                    If Application.Wait(Now + TimeValue("0:00:1")) Then Exit Do 'wait for 1 sec
                Loop
            Next
        Next
    Next
End Sub

wbr Janos
0
 

Author Comment

by:tchristie33
ID: 20312464
I am going to have to perform this for a  few shapes in my workbook. I wan to select a shape that I have already created and named and then turn it a color based on the criteria of another cell. Thanks for the help

Sub Shape()
Sheets("Performance-Reliability").Select
Dim shp As Shape
Shapes("Oval 1").Select
If Range("a1") < 25 Then
shp.DrawingObject.Interior.Color = vbRed
End If
Shapes("Oval 1").Select
If Range("a1") > 25 Then
shp.DrawingObject.Interior.Color = vbGreen
End If
Shapes("Oval 1").Select
If Range("a1") = 25 Then
shp.DrawingObject.Interior.Color = vbYellow
End If
End Sub
0
 
LVL 10

Accepted Solution

by:
János Szabó earned 500 total points
ID: 20314134
If you know the name of the shape ("YourShape") you can simply change it's color by:

ActiveSheet.Shapes("YourShape").Fill.ForeColor.RGB = RGB(Red,Green,Blue)

where each variable of Red, Green and Blue have a value between 0 to 255
0
 
LVL 10

Expert Comment

by:János Szabó
ID: 20314242
you can use the Select Case statement. Use it on the sheet's macro page

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim shp As Shape
    Sheets("Performance-Reliability").Select
    With ActiveSheet.Shapes("Oval 1").DrawingObject.Interior
    Select Case Cells(1, 1).Value
        Case Is < 25
            .Color = vbRed
        Case Is > 25
            .Color = vbGreen
        Case Else
            .Color = vbYellow
    End Select

End Sub
0
 

Author Closing Comment

by:tchristie33
ID: 31409648
I loved that it was easy and simple to understand
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question