Solved

Excel VBA - format a Shape same as a Cell

Posted on 2016-11-17
7
96 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
[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
  • 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
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!

 

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
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 using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

687 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