Archive of Mr Excel Message Board


Back to Controls in Excel archive index
Back to archive home



Changing the color of a command button in Excel

Posted by Kurt on October 13, 2000 2:08 PM
Hello everyone!!

How do I change the color of a command button in Excel?

I have the following two lines of code in a subroutine

fmNode.but_nodenext.SetFocus
fmNode.but_nodenext.BackColor = RGB(0, 0, 100)

I have a command button that I want changed to a color
whenever the focus is shifted to the Next commmand button

Any ideas. Thanks in advance.

Kurt


Re: Changing the color of a command button in Excel

Posted by Ivan Moala on October 14, 2000 8:09 PM

Kurt
If you want the command buttons to change color
when they have the focus then you need to look
@ the Enter & Exit events for tha commandbuttons.
Example;

Notes:
1) 3 commandbuttons
2) commandbuttons are named
CommandButton1 - 3
If you have named your commandbuttons
then you will have to change the routine
using a select case and the name of your
buttons.
3) The Enter & Exit is similar to the HasFocus
& LostFocus for a ActiveX object....the events
you would need for this.

Private Sub CommandButton1_Enter()
Call ChangeBtnColor
End Sub

Private Sub CommandButton1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Call ChangeBtnColor
End Sub

Private Sub CommandButton2_Enter()
Call ChangeBtnColor
End Sub

Private Sub CommandButton2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Call ChangeBtnColor
End Sub

Private Sub CommandButton3_Enter()
Call ChangeBtnColor
End Sub

Private Sub CommandButton3_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Call ChangeBtnColor
End Sub

Private Sub ChangeBtnColor()
'Exit Sub
Dim obj As Object

Set obj = UserForm1.ActiveControl

If obj.Name Like "Comm*" Then 'Is it a commandbutton
If obj.BackColor = &HFF& Then 'Is it Red
obj.BackColor = &H8000000F 'Change to default color
Else
obj.BackColor = &HFF& 'Change to Red
End If
End If
Set obj = Nothing
End Sub

HTH

Ivan

Kurt






This archive is from the original message board at www.MrExcel.com.
All contents © 1998-2004 MrExcel.com.
Visit our online store to buy searchable CD's with thousands of VBA and Excel answers.
Microsoft Excel is a registered trademark of the Microsoft Corporation.
MrExcel is a registered trademark of Tickling Keys, Inc.