Solved

Excel 2013 Shapes Color mystery

Posted on 2014-12-01
3
161 Views
Last Modified: 2014-12-06
What I'm trying to do is
ActiveSheet.Shapes("shpPart1").Fill.ForeColor = 13998939

Open in new window


but I get a Type Mismatch error.

Can someone please explain why I'm getting this? I see that the ForeColor is a long integer but I get a Type Mismatch error. So I tried String data type and get the same error.

Sub PartShapesTEST2()

    Dim s As Shape
    Dim lngColor As Long
    
    Set s = ActiveSheet.Shapes("shpPart1")
    
    lngColor = s.Fill.ForeColor
    
    s.Fill.ForeColor = lngColor
    
End Sub

Open in new window

0
Comment
Question by:hindersaliva
3 Comments
 

Author Comment

by:hindersaliva
ID: 40474227
Solved it!

Should be

s.Fill.ForeColor.RGB = lngColor

even though the color is not in RGB ie. not as in (255, 0, 0)
0
 
LVL 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 250 total points
ID: 40474859
The ForeColor property actually returns a ColorFormat object, but because you have declared lngColor as Long, the VB engine converts to the Long RGB representation.

The Long number you are seeing is actually an RGB value, but uses a mathematical equation (R*256^2 + G*256 + B) to convert it.

An alternative to your original code would be as follows...

Sub PartShapesTEST2()

    Dim s As Shape
    Dim oColor As ColorFormat
    
    Set s = ActiveSheet.Shapes("shpPart1")
    
    Set oColor = s.Fill.ForeColor
    
    s.Fill.ForeColor = oColor
    
End Sub

Open in new window

0
 
LVL 5

Assisted Solution

by:Hakan Yılmaz
Hakan Yılmaz earned 250 total points
ID: 40475801
You can also change color this way,
YourShapeObject.Fill.ForeColor.RGB = RGB(redvalue 0-255, greenvalue 0-255, bluevalue 0-255)

RGB in the right hand side is a built in function that returns Long Integer number representing the color you gave.
So you can assign Long Integer value to a Long Integer property of an object.

You can use Object Browser in Excel (Shortcut F2) to see what type of object you should assign to a property of an another object.
If you can't find enough explanations in Object Browser, you may look at Excel 2010 Developer Reference or Excel 2013 Developer Reference.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

21 Experts available now in Live!

Get 1:1 Help Now