Solved

I want to write a macro for a shape in Excel

Posted on 2007-11-16
9
1,926 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:kacor
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:kacor
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
 
LVL 33

Expert Comment

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


0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 10

Expert Comment

by:kacor
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:
kacor 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:kacor
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now