Activate Caps Lock When a Workbook Opens and When the Workbook is made Active

CADcliff

New Member
Joined
Jul 3, 2018
Messages
6
I want to assure all my data entry in this Excel Workbook is Uppercase. I am navigating back and forth to other programs in the process. Is there any way I can capture the current CAPS Lock status, then turn on CAPS lock when I open the workbook? I would like to be able to "restore" the captured CAPS lock when another program window is made active, and then turn CAPS lock back on when I reactivate the Excel workbook window. I found an old thread from 2004, but the instructions and code there do not seem to work for me in Windows 10 and Excel 2016. I may use this in many of my Excel workbooks if We find something that works. I have looked at the all uppercase fonts, but they are not really suitable for clean data display. Thank you for any help!
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
You could consider establishing a SheetChange event procedure for the ThisWorkbook code module which would automatically upper case any value entered into a cell on any worksheet in the workbook (that way you would not have to concern yourself with the CAPS LOCK key)...
VBA Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
  Application.EnableEvents = False
  Target.Value = UCase(Target.Value)
  Application.EnableEvents = True
End Sub
 
Upvote 0
Thanks Rick. That is a good suggestion. I am currently using something similar.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.HasFormula Then Exit Sub
        Application.EnableEvents = False
        Target = UCase(Target.Cells(1))
    Application.EnableEvents = True
End Sub

I guess I'm a little OCD about data entry. I wanted to set up the Excel sheet to help me (make me) enter the data correctly. I was frustrated to find code to turn CAPS lock off and on, but not be able to figure out how to use it.
 
Upvote 0
This is what I found from 2004. But I can't figure out how to make it work. Either something has changed in Excel, or I am putting the code in the wrong places.


Tom Urtis says:

In the workbook module:

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Private Sub Workbook_Open()
Run "CapsOn"
Application.OnKey "{CAPSLOCK}", "CapsOn"
End Sub

Private Sub Workbook_Activate()
Run "CapsOn"
Application.OnKey "{CAPSLOCK}", "CapsOn"
End Sub

Private Sub Workbook_WindowActivate(ByVal Wn As Window)
Run "CapsOn"
Application.OnKey "{CAPSLOCK}", "CapsOn"
End Sub



Private Sub Workbook_BeforeClose(Cancel As Boolean)
Run "CapsOff"
Application.OnKey "{CAPSLOCK}"
End Sub

Private Sub Workbook_Deactivate()
Run "CapsOff"
Application.OnKey "{CAPSLOCK}"
End Sub

Private Sub Workbook_WindowDeactivate(ByVal Wn As Window)
Run "CapsOff"
Application.OnKey "{CAPSLOCK}"
End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''



In a standard module:

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Declare Function GetKeyboardState Lib "user32" (pbKeyState As Byte) As Long
Declare Function SetKeyboardState Lib "user32" (lppbKeyState As Byte) As Long

Sub CapsOff()
Dim Res As Long
Dim KBState(0 To 255) As Byte
Res = GetKeyboardState(KBState(0))
KBState(&H14) = 0
Res = SetKeyboardState(KBState(0))
End Sub

Sub CapsOn()
Dim Res As Long
Dim KBState(0 To 255) As Byte
Res = GetKeyboardState(KBState(0))
KBState(&H14) = 1
Res = SetKeyboardState(KBState(0))
End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''


Worked for me when I tested it (Excel2003 WXP) but it's still early on a Sunday in California...see if it does what you need and post back if problems.

Tom Urtis
Microsoft Excel MVP 2008-2020
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,267
Members
449,075
Latest member
staticfluids

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
Back
Top