Solved

Excel VBA - format a Shape same as a Cell

Posted on 2016-11-17
7
57 Views
Last Modified: 2016-11-18
I'm trying to create a simple Highlight ON/OFF on a Shape.

In the attached example, when the Turn ON button is clicked I want the Shape 'Save' to take the format of the 'On' cell C6. When the Turn OFF is clicked I want it to be like D6, including the Border color as Outline.

(Recording the macro didn't quite give me the answer, as I want to be able to format C6 and D6 in any color, but shapes seem to want to know only RGB)

Thanks.
Toggle-Button.xlsm
0
Comment
Question by:hindersaliva
  • 4
  • 2
7 Comments
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 41892391
What do you mean by the "Turn ON" and "Turn OFF" buttons? I see only one shape on the worksheet named "btnSave".
0
 
LVL 81

Expert Comment

by:byundt
ID: 41892466
I revised your macro so it would toggle the color on the Save button between red and black:
Sub ToggleColor()
With ActiveSheet.Shapes("Rounded Rectangle 1").Fill
        .Visible = msoTrue
        If .ForeColor.RGB = RGB(0, 0, 0) Then
            .ForeColor.RGB = RGB(192, 0, 0)
        Else
            .ForeColor.RGB = RGB(0, 0, 0)
        End If
End With
End Sub

Open in new window

I would also suggest that you check the box for "Require variable declaration" in the Tools...Options...Editor menu item in the VBA Editor. This will put Option Explicit at the top of every newly created module sheet, and is considered good practice in VBA programming. Option Explicit means you must put every variable in a Dim statement. VBA can then catch typos in variable names for you.
0
 
LVL 81

Expert Comment

by:byundt
ID: 41892471
I see that you want to make the Save toggle between the appearance of the shapes at C6 and D6. If so, the code becomes:
Sub ToggleColor()
Dim shp As Shape
With ActiveSheet.Shapes("Rounded Rectangle 1")
        .Fill.Visible = msoTrue
        If .Fill.ForeColor.RGB <> RGB(192, 0, 0) Then
            .Fill.ForeColor.RGB = RGB(192, 0, 0)
            .TextFrame2.TextRange.Characters.Font.Fill.ForeColor.RGB = RGB(255, 255, 255)
            .Line.Visible = msoFalse
        Else
            .Fill.ForeColor.ObjectThemeColor = msoThemeColorBackground1
            .TextFrame2.TextRange.Characters.Font.Fill.ForeColor.RGB = RGB(0, 0, 0)
            .Line.ForeColor.RGB = RGB(0, 176, 240)
            .Line.Visible = msoTrue
        End If
End With
End Sub

Open in new window

If you want to determine the color of the fill or outline by reference to a cell, put the following statements in the Immediate Window of the VBA Editor. They will return the color as a Long. You can then use those values in your code as Color = qqqqqqqq instead of Color.RGB = RGB(xxx, yyy, zzz)
?ActiveCell.Interior.Color
?ActiveCell.Borders.Color
-Toggle-ButtonQ28983915.xlsm
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:hindersaliva
ID: 41892742
Byundt, I get the color 192 for the cell fill color. But the assignment for the Shape is RGB. So wouldn't I need to 'translate/convert' the 192 to RGB in order to apply it to the Shape?

For example, if the cell fill color happens to be 42,875?

Thanks.

Wayne Taylor: sorry I attached before I saved the example! It was just a button to fire off the code.
0
 
LVL 81

Accepted Solution

by:
byundt earned 500 total points
ID: 41893067
I misspoke on how to use the Color value directly in the code. Since the RGB function returns a Long number, you would use Color.RGB = 42875 to set the color property to a lime green.
Sub ToggleColor()
Dim shp As Shape
With ActiveSheet.Shapes("Rounded Rectangle 1")
        .Fill.Visible = msoTrue
'        If .Fill.ForeColor.RGB <> 192      'RGB(192, 0, 0) Then      'Red
'            .Fill.ForeColor.RGB = 192      'RGB(192, 0, 0)
        If .Fill.ForeColor.RGB <> 42875 Then                'Lime green
            .Fill.ForeColor.RGB = 42875
            .TextFrame2.TextRange.Characters.Font.Fill.ForeColor.RGB = 16777215     'RGB(255, 255, 255)
            .Line.Visible = msoFalse
        Else
            .Fill.ForeColor.ObjectThemeColor = msoThemeColorBackground1
            .TextFrame2.TextRange.Characters.Font.Fill.ForeColor.RGB = 0    'RGB(0, 0, 0)
            .Line.ForeColor.RGB = 15773696      'RGB(0, 176, 240)
            .Line.Visible = msoTrue
        End If
End With
End Sub

Open in new window

-Toggle-ButtonQ28983915.xlsm
0
 

Author Closing Comment

by:hindersaliva
ID: 41893135
Thanks Byundt
0
 
LVL 81

Expert Comment

by:byundt
ID: 41893163
You may find the following function useful in converting a Long Color into an RGB function call. It takes the Color number, and breaks it down into the R, G and B components. You should install the function in a regular module sheet, just like it was a macro.

To use it, array-enter a formula like the following in three adjacent cells in a row. It should return 167, 123 and 0 in those cells.
=GetRGB(42875)
Function GetRGB(lngColor) As Variant
Dim R As Long, G As Long, B As Long
R = lngColor Mod 256
G = (lngColor - R) / 256 Mod 256
B = (lngColor - R - G * 256) / 256 ^ 2
GetRGB = Array(R, G, B)
End Function

Open in new window

1

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

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