Solved

Excel 2013 Shapes Color mystery

Posted on 2014-12-01
3
165 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

707 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