Click event, CommandButton control, Accelerator, Caption properties example

This example changes the Accelerator and Caption properties of a CommandButton each time the user clicks the button by using the mouse or the accelerator key. The Click event contains the code to change the Accelerator and Caption properties.

To try this example, paste the code into the Declarations section of a form containing a CommandButton named CommandButton1.

Private Sub UserForm_Initialize() 
 CommandButton1.Accelerator = "C" 
 'Set Accelerator key to COMMAND + C 
End Sub 
 
Private Sub CommandButton1_Click () 
 If CommandButton1.Caption = "OK" Then 
 'Check caption, then change it. 
 CommandButton1.Caption = "Clicked" 
 CommandButton1.Accelerator = "C" 
 'Set Accelerator key to COMMAND + C 
 Else 
 CommandButton1.Caption = "OK" 
 CommandButton1.Accelerator = "O" 
 'Set Accelerator key to COMMAND + O 
 End If 
End Sub

Support and feedback

Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.