ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   Checkbox to change background color, font color and remove/ add bo (https://www.excelbanter.com/excel-discussion-misc-queries/209832-checkbox-change-background-color-font-color-remove-add-bo.html)

sara

Checkbox to change background color, font color and remove/ add bo
 
I am trying to make checkbox1 change font color, background color and
add/remove border lines if possible.

I am really not sure how to write the code for this at all. I would like to
change the font from black to yellow, and the background color in cell A3,
B3, C3 (one merged cell) from white to yellow. I would also like the black
border line around the cell to change as well, but I am not sure if that is
possible?

Thanks.

ExcelBanter AI

Answer: Checkbox to change background color, font color and remove/ add bo
 
Sure, I can help you with that!

To change the font color and background color of a cell based on the value of a checkbox, you can use a simple VBA code. Here are the steps:
  1. Right-click on the checkbox and select "Format Control".
  2. In the "Control" tab, enter a cell link where the value of the checkbox will be stored. For example, you can enter "D3".
  3. Close the "Format Control" dialog box.
  4. Press "Alt + F11" to open the VBA editor.
  5. In the editor, double-click on the sheet where the checkbox is located.
  6. Paste the following code:

    Formula:

    Private Sub CheckBox1_Click()
        If 
    Range("D3").Value True Then
            
    [b]Range("A3:C3").Interior.Color RGB(2552550)[/b'change background color to yellow
            [b]Range("A3:C3").Font.Color = RGB(255, 255, 0)[/b] '
    change font color to yellow
            
    [b]Range("A3:C3").Borders.LineStyle xlNone[/b'remove border lines
        Else
            [b]Range("A3:C3").Interior.Color = RGB(255, 255, 255)[/b] '
    change background color to white
            
    [b]Range("A3:C3").Font.Color RGB(000)[/b'change font color to black
            [b]Range("A3:C3").Borders.LineStyle = xlContinuous[/b] '
    add border lines
        End 
    If
    End Sub 

  7. Save the workbook and close the VBA editor.

Now, when you click on the checkbox, the font color and background color of cell A3, B3, C3 will change to yellow, and the border lines will be removed. When you uncheck the checkbox, the font color and background color will change back to black and white respectively, and the border lines will be added back.

Tom Hutchins

Checkbox to change background color, font color and remove/ add bo
 
Select View Toolbars Control Toolbox to display the Control Toolbox
toolbar. Add a check box to your worksheet. Right-click on the checkbox and
select View Code. The Visual Basic Editor (VBE) is displayed with an empty
subroutine for the Click event for the checkbox. Probably the following:

Private Sub CheckBox1_Click()
End Sub

Paste the following code in the subroutine (between those two lines):

Range("A3:C3").Select
If CheckBox1.Value = True Then
With Selection.Interior
.ColorIndex = 6
.Pattern = xlSolid
End With
Selection.Font.ColorIndex = 6
With Selection.Borders(xlEdgeLeft)
.ColorIndex = 6
End With
With Selection.Borders(xlEdgeTop)
.ColorIndex = 6
End With
With Selection.Borders(xlEdgeBottom)
.ColorIndex = 6
End With
With Selection.Borders(xlEdgeRight)
.ColorIndex = 6
End With
Else
With Selection.Interior
.ColorIndex = xlAutomatic
.Pattern = xlSolid
End With
Selection.Font.ColorIndex = xlAutomatic
With Selection.Borders(xlEdgeLeft)
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.ColorIndex = xlAutomatic
End With
End If

This is basically code I recorded with the macro recorder. It will color the
merged cells' font, background, and borders yellow when the checkbox is
checked. Unchecking the checkbox restores the cells to the default black on
white colors.

Save the workbook, then select File Close (in the VBE) to return to
regular Excel. Click the 'Exit Design Mode' button on the Control Toolbox
toolbar (probably the first button), and try out your new checkbox.

If you are new to macros, this link to Jon Peltier's site may be helpful:
http://peltiertech.com/WordPress/200...e-elses-macro/

Hope this helps,

Hutch

"Sara" wrote:

I am trying to make checkbox1 change font color, background color and
add/remove border lines if possible.

I am really not sure how to write the code for this at all. I would like to
change the font from black to yellow, and the background color in cell A3,
B3, C3 (one merged cell) from white to yellow. I would also like the black
border line around the cell to change as well, but I am not sure if that is
possible?

Thanks.



All times are GMT +1. The time now is 09:48 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com