Keyboard shortcut to change textbox background color

CSyx

Board Regular
Joined
May 2, 2012
Messages
77
Good day all,

Is it possible to assign a keyboard shortcut to change a userform textbox background color? If so, could you please point me in the right direction? The below code works for a textbox when assigned to a command button on the userform. However, it does not work when assigned to a keyboard shortcut.

Code:
If UserForm1.TextBox1.BackColor = &HFFFFFF Then
        UserForm1.TextBox1.BackColor = &HFFFF&
Else
        UserForm1.TextBox1.BackColor = &HFFFFFF
End If

Thanks for the help!
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,961
Office Version
  1. 365
Platform
  1. Windows
Here is one way:

Put a control button on your form.
Put your code in for that button.
Here is a shortened version of your code:
Code:
Private Sub CommandButton1_Click()
UserForm1.TextBox1.BackColor = IIf(UserForm1.TextBox1.BackColor = &HFFFFFF, &HFFFF00, &HFFFFFF)
End Sub
Set the Accelerator property to a single letter.
Set the Height and Width of this control button to 0
So if you used t as your accelerator, you would hit Alt-t to run your code.
 
Upvote 0

Forum statistics

Threads
1,191,187
Messages
5,985,194
Members
439,947
Latest member
fabiannic

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top