Forum Discussion

Chris_555's avatar
Chris_555
Copper Contributor
Jun 15, 2023

Control Button

I would like to change to change the fill colour of my control buttons is that possible

  • NikolinoDE's avatar
    NikolinoDE
    Gold Contributor

    Chris_555 

    Yes, it is possible to change the fill color of control buttons in Excel using VBA macros.

    Here is an example (Applicable to Form Controls) of how you can achieve this:

    1. Open the Excel workbook where you have the control button.
    2. Press "Alt+F11" to open the Visual Basic Editor.
    3. In the Project Explorer window on the left side, locate and double-click on the sheet module that contains the control button (e.g., Sheet1).
    4. In the code window that opens, paste the following VBA code:
    Sub ChangeButtonColor()
        Dim btn As Button
        
        ' Replace "Button 1" with the name of your control button
        Set btn = Sheet1.Buttons("Button 1")
        
        ' Replace RGB(255, 0, 0) with the desired RGB color code
        btn.BackColor = RGB(255, 0, 0)
    End Sub

    5. Customize the code as follows:

      • Replace "Button 1" with the actual name of your control button. To check or change the name of the control button, right-click on it, select "Edit Text", and modify the name in the "Name" field of the Properties window.
      • Replace RGB(255, 0, 0) with the desired RGB color code. You can use online color picker tools to find the RGB values for the color you want 

    6. Close the Visual Basic Editor.

    7. Press "Alt+F8" to open the Macros dialog.

    8. Select the "ChangeButtonColor" macro from the list and click the "Run" button.

    The macro will run and change the fill color of the specified control button to the chosen RGB color. You can modify the code or create additional macros to change the color of different buttons or perform other customizations as needed.

    Note that the code assumes the control button is located on Sheet1. If your button is on a different sheet, you need to modify the code accordingly by changing Sheet1 to the appropriate sheet name or index.

    Remember to save your workbook as a macro-enabled (.xlsm) file format to preserve the VBA code.

     

    Here is how you can change the fill color of an ActiveX control button using VBA:

    1. Open the Excel workbook where you have the ActiveX control button.
    2. Press "Alt+F11" to open the Visual Basic Editor.
    3. In the Project Explorer window on the left side, locate and double-click on the sheet module that contains the ActiveX control button (e.g., Sheet1).
    4. In the code window that opens, paste the following VBA code:
    Sub ChangeActiveXButtonColor()
        Dim btn As OLEObject
        
        ' Replace "Button1" with the actual name of your ActiveX control button
        Set btn = Sheet1.OLEObjects("Button1")
        
        ' Replace RGB(255, 0, 0) with the desired RGB color code
        btn.Object.BackColor = RGB(255, 0, 0)
    End Sub

    5. Customize the code as follows:

      • Replace "Button1" with the actual name of your ActiveX control button. To check or change the name of the ActiveX control button, right-click on it, select "Properties", and modify the name in the "Name" field of the Properties window.
      • Replace RGB(255, 0, 0) with the desired RGB color code.

    6. Close the Visual Basic Editor.

    7. Press "Alt+F8" to open the Macros dialog.

    8. Select the "ChangeActiveXButtonColor" macro from the list and click the "Run" button.

    The macro will run and change the fill color of the specified ActiveX control button to the chosen RGB color. Again, you can modify the code or create additional macros to change the color of different ActiveX buttons or perform other customizations as needed.

     

Resources