VBA Remember Numlock State

L

Legacy 143009

Guest
Hi Guys!

I have a macro that formats a UserForm TextBox. If there is a character out of format, then it sends backspace key. But somehow, it turns off the numlock sometimes. Now what i need is something which will recall the previous numlock state. Like;

Code:
NState = Numlock Key Status

'My Code goes here

Numlock Key Status = NState
Thanks a lot!
 
Last edited by a moderator:

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
copy this to a regular module

Code:
Public Const VK_NUMLOCK = &H90

Public Declare Function GetKeyState Lib "user32" _
(ByVal nVirtKey As Long) As Long


Sub Test()
MsgBox "Numlock is " & IIf(GetKeyState(VK_NUMLOCK) = 1, "On", "Off")
End Sub
 
Upvote 0
Simpler solution - don't use Sendkeys. ;)
 
Upvote 0
adapt this to suit. it will only allow numbers to be entered , plus one period(.)
Code:
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
    Case Asc("0") To Asc("9")
    Case Asc(".") ' only allow one decimal point to be used
        If InStr(1, Me.TextBox1.Text, ".") > 0 Then
            KeyAscii = 0
        End If
    Case Else
        KeyAscii = 0
End Select

End Sub
 
Upvote 0
Personally I just test if it's numeric after the user has finished entering data.
 
Upvote 0
Case Asc("0") To Asc("9"), Asc("-")


will allow a minus as well, yes
 
Upvote 0
copy this to a regular module

Code:
Public Const VK_NUMLOCK = &H90

Public Declare Function GetKeyState Lib "user32" _
(ByVal nVirtKey As Long) As Long


Sub Test()
MsgBox "Numlock is " & IIf(GetKeyState(VK_NUMLOCK) = 1, "On", "Off")
End Sub


I used a version of this code to work around the numlock issue with "SendKeys". It works great on 32 bit installations of excel, but I have users using my workbook on both 32 bit and 64 bit installations. I used "PtrSafe" in my declaration which at least allows the file to open in 64 bit installations, but I still have the numlock toggling on and off while my code runs and not getting left in the desired state. Here is my code:

Public Declare PtrSafe Function GetKeyState Lib "user32" _
(ByVal nVirtKey As Long) As Long
Public Const VK_NUMLOCK = &H90


NumLockState = GetKeyState(VK_NUMLOCK)

'my code here

If NumLockState <> GetKeyState(VK_NUMLOCK) Then
Application.SendKeys ("%{Numlock}"), True
End If


I am looking for a solution that will leave the numlock state in the same state that the code started on in both 32bit and 64bit installations of excel.

Thanks.
 
Upvote 0

Forum statistics

Threads
1,216,077
Messages
6,128,685
Members
449,463
Latest member
Jojomen56

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