I am developing a userform in EXCEL 2016 VBA with a number of RefEdit boxs. When I use one of the MS developed userforms (like, for example, the Descriptive Statistics userform in the Data Analysis ToolPak), the user is able to use shortcut keys like Shift + ↓ or Shift + Ctrl + ↓ to make the selection.
At present, my RefEdit boxes do not support this function. Is it possible to code these attributes in VBA for a typical RefEdit box? If so, can someone please provide example code?
Many thanks,
Dan
I just created a dummy UserForm with a RefEdit, and I see that the arrow keys work in conjunction with Shift and Ctrl as expected, but only if the UserForm hasn't been shrunken to show only the RefEdit.
I've used the _KeyDown
event in a UserForm to capture when a user clicks the keyboard arrow keys. Maybe you could try the _KeyDown
event of the RefEdit.
Private Sub RefEdit1_KeyDown(KeyCode As Integer, ByVal Shift As Integer)
End Sub
It's going to be pretty complicated, because you're coing to have to see what's indicated in the RefEdit, and what was initially in the RefEdit, so you know whetehr the arrow keys expand or shrink the range in a particular direction.
A further complication is that a RefEdit_KeyDown
is different from other _KeyDown
events. _KeyDown
for a textbox looks like this:
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
End Sub
Notice that KeyCode is of type MSForms.ReturnInteger
, while in the RefEdit, it is type Integer
. What's that mean? Well, the arrow keys Left, Up, Right, and Down have ReturnInteger values of 37, 38, 39, and 40. These keys have Integer values of 0, 0, 0, and 0. So using KeyCode As Integer
, the RefEdit can't distinguish which arrow key was clicked (or which other value=0 keys may have been clicked, if they exist).
If you try to change the RefEdit1_KeyDown
event to use ReturnInteger
, you get a compile error. And if the RefEdit has focus, it receives the _KeyDown
event, so you can't use _KeyDown
for any other control to determine which arrow was clicked.
I don't use RefEdits in mission-critical projects. It is simply too flaky. Sometimes it doesn't even appear in UserForms on some users' computers, and often it just behaves in a flaky way. I've described a different approach in Alternative to Excel's Flaky RefEdit Control . That approach also does not support the arrow keys.
I run the following code when I initialize any form with RefEdit
Objects. FixedRefEditKeys
is just a Global Boolean
Note: Cannot use keyboard shortcuts to select ranges in RefEdit control in Excel
Public Sub FixRefEditKeys()
Dim sKey As String
'http://support.microsoft.com/kb/291110
If Not FixedRefEditKeys Then
sKey = "HKEY_CURRENT_USER\software\microsoft\office\" & Application.Version & "\Excel\Options\"
If Not RegKeyExists(sKey & "QFE_Richmond") Then RegKeySave sKey & "QFE_Richmond", 1, "REG_DWORD"
End If
End Sub
Which uses the following from "my" (aka the Internet) Registry
Module:
'returns True if the registry key i_RegKey was found
'AND False if not
Function RegKeyExists(i_RegKey As String) As Boolean
Dim myWS As Object
On Error GoTo ErrorHandler
'access Windows scripting
Set myWS = CreateObject("WScript.Shell")
'try to read the registry key
myWS.RegRead i_RegKey
'key was found
RegKeyExists = True
Exit Function
ErrorHandler:
'key was not found
RegKeyExists = False
End Function
'sets the registry key i_RegKey to the
'value i_Value with type i_Type
'if i_Type is omitted, the value will be saved as string
'if i_RegKey wasn't found, a new registry key will be created
Sub RegKeySave(i_RegKey As String, _
i_Value As String, _
Optional i_Type As String = "REG_SZ")
Dim myWS As Object
'access Windows scripting
Set myWS = CreateObject("WScript.Shell")
'write registry key
myWS.RegWrite i_RegKey, i_Value, i_Type
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.