Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Streamline VBA to Change shape color on click in excel

Tags:

excel

shapes

vba

I have a ‘form’ that contains set of questions on a worksheet (note this is not a userform and I don’t want to use one). Some answers are yes/no, others have multiple answers such as quantity (ie and the answer may be 1 or 2 or 3 or 4 etc).

The design of the ‘form’ on this worksheet calls for these answers to be shapes which the user clicks like a button to select their answer - Pls note I do not want to use Command buttons.

In this simple example I have 2 rectangle shapes one name “yes” and one name “no” When user clicks “yes”, the color fill of the shape changes to blue (and the “no” shape stays white). If user clicks “no” , the “no” shape turns blue, and “yes” goes white. It also populates and answer in A1 in this example.

I use the following code which works fine (although im sure could be cut down somewhat) however the problem comes when I need to replicate this code multiple times. For example, if I have a question that has multiple answers like Quantity (answers could be 1 or 2 or 3 or 4 or 5) then each macro (ie for button “1” ) needs and “active” piece of coder, and a “non active” piece to designate colours to the active shape and all the other non active shapes. This is very repetitive and the code quickly becomes verbose. Im hoping there is a way to keep the formatting (fill color, text color etc) in a separate macro such as “Sub Active” and “Sub Non_Active” rather than having to repeat it time after time. I’ve tried to use “Call” to grab the macro containing the formatting (like Call Active) but keep getting an error.

Sub yes_button()

'active
ActiveSheet.Shapes("yes").Select
ActiveSheet.Shapes("yes").Fill.ForeColor.RGB = RGB(85, 142, 213)                          ' fill: dark blue color
ActiveSheet.Shapes("yes").Line.BackColor.RGB = RGB(198, 217, 241)                        ' border: light blue color
ActiveSheet.Shapes("yes").TextFrame.Characters.Font.Color = RGB(255, 255, 255)       '         text: white color
Range("A1").Formula = "YES" ' fills cell with button value

' nonactive
ActiveSheet.Shapes("no").Select
ActiveSheet.Shapes("no").Fill.ForeColor.RGB = RGB(255, 255, 255)                  '     fill: light blue color
ActiveSheet.Shapes("no").Line.BackColor.RGB = RGB(198, 217, 241)                  ' border: light blue color
ActiveSheet.Shapes("no").TextFrame.Characters.Font.Color = RGB(85, 142, 213)     ' text: dark blue color



End Sub

Sub no_button()

'active
ActiveSheet.Shapes("no").Select
ActiveSheet.Shapes("no").Fill.ForeColor.RGB = RGB(85, 142, 213)                       '     fill: dark blue color
ActiveSheet.Shapes("no").Line.BackColor.RGB = RGB(198, 217, 241)                      '    border: light blue color
ActiveSheet.Shapes("no").TextFrame.Characters.Font.Color = RGB(255, 255, 255)       ' text: white color
Range("A1").Formula = "NO" ' fill scell with button value
' nonactive

ActiveSheet.Shapes("yes").Select
ActiveSheet.Shapes("yes").Fill.ForeColor.RGB = RGB(255, 255, 255)                  ' fill: light blue color
ActiveSheet.Shapes("yes").Line.BackColor.RGB = RGB(198, 217, 241)                  '     border: light blue color
ActiveSheet.Shapes("yes").TextFrame.Characters.Font.Color = RGB(85, 142, 213)     '     text: dark blue color

End Sub

Would appreciate any suggestions. Thankyou

like image 995
user1872463 Avatar asked Jul 15 '14 05:07

user1872463


People also ask

How do you change the color of a shape in Excel?

Select the shape or text box. On the Drawing Tools Format tab, click Shape Outline, and then click More Outline Colors. In the Colors box, either click the color that you want on the Standard tab, or mix your own color on the Custom tab.

How to change cell color when cell is clicked with VBA?

Change cell color when cell is clicked with VBA code. Here, you can change the background color of a cell when double clicking it or right clicking on it with the following VBA code. 1. In the worksheet you will change the cell color when clicking on it, right click the sheet tab and click View Code from the right-clicking menu. 2.

How to change shape color instantly when value in any cell changes?

Click Alt+F11 to open the VBA editor. From the Project Explorer double click the Sheet where you have the data. You can also right click the sheet tab and choose View Code. Write the code inside Worksheet_Change event. Since, I want to change the shape’s color instantly when the value in any cell changes.

How to colour a shape in Excel using VBA?

The VBA procedure does this by changing the value in F9 from 1 to 2. The following is the VBA to achieve the task. Sub ColourShp () 'Excel VBA procedure to colour a shape. The attached file shows the workings of the above VBA procedure.

How to change the background color of a cell in Excel?

Here, you can change the background color of a cell when double clicking it or right clicking on it with the following VBA code. 1. In the worksheet you will change the cell color when clicking on it, right click the sheet tab and click View Code from the right-clicking menu.


1 Answers

yes, you're right, you could write a Sub with your shape as an input and eventually fill it with the "yes" and "no" events. E.g. ClickOnButton MyShape, YesNo where YesNo can be a flag that triggers one of the events. Then you could call that Sub for each button.

I also would suggest the use of some Withs: With Activesheet.MyShape is going to do fine. Finally, please do not use the .Select. There are tons of reason not to do that and most of all the select won't do really anything in your code... Well yeah, slow it down.

I'll give you an example to try to explain better: You could write a subroutine giving a Shape and a Boolean (for example) as an Input (that would be the YesNo variable). Inside the subroutine you could write the 2 different behaviours conditionally (If ... Else ... End If) to the YesNo variable (or, do we want to call it GreenRed/ActiveInactive?). In both conditions you can write whatever you want. The following can be used for both "yes" and "no" buttons.

Sub Example(YourShape As Shape, GreenRed as Boolean)

    If GreenRed = True Then ' Say we want in this case an "active" button
        With YourShape
            .Fill.ForeColor.RGB = RGB(85, 142, 213)
            .Line.BackColor.RGB = RGB(198, 217, 241)
            .TextFrame.Characters.Font.Color = RGB(255, 255, 255)
        End With
    Else
        With YourShape
            .Fill.ForeColor.RGB = RGB(255, 255, 255)
            .Line.BackColor.RGB = RGB(198, 217, 241)
            .TextFrame.Characters.Font.Color = RGB(85, 142, 213) 
        End With
    End If

End Sub

You can then in your Main program write Example ActiveSheet.Shapes("yes"), True to get a button activate itself and Example ActiveSheet.Shapes("no"), False to deactivate the other.

like image 137
Noldor130884 Avatar answered Oct 14 '22 16:10

Noldor130884