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
Solved

Excel 2013 Shapes Color mystery

Posted on 2014-12-01
3
163 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

856 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