Keep Key Down/Up in Userform Textbox

Nadine67

Board Regular
Joined
May 27, 2015
Messages
225
Hello and thank you for any attention my post may receive.

I have a Userform with 24 textboxes and multiple comboboxes. The problem I have is when using the key down/up to read the wrapped text in a textbox the cursor leaves the textbox and enters combobox. if I don't realise this and continue to press the arrow key the combobox value changes.

I have this code in the Userform but it does not keep the arrow key in the textbox. I also thought of trying to restrict moving about the userform to only tab or mouse function but am not sure if that is the best solution.

Any help or suggestions would be greatly appreciated.

Code:
Private Sub Userform_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    If KeyCode = vbKeyDown Then
        With Me.TextBox
            If .LineCount > 1 And _
            .CurLine = .LineCount - 1 Then
                Beep
                .SetFocus
                .CurLine = .LineCount - 2
                .SetFocus
            End If
        End With
    End If
    If KeyCode = vbKeyUp Then
        With Me.TextBox
            If .LineCount > 1 And _
            .CurLine = 0 Then
                Beep
                .SetFocus
                .CurLine = 1
            End If
        End With
    End If
End Sub

Thank you and have a great day!
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
I think I may have solved my issue.

After leaving the textbox my code traps the curser in the last combo box (directly above the text box) thus disabling it. And so if there was a combo box directly below the last text box then I would place the same code in it.

Maybe not the most ideal solution but at least it doesn't change the combo box selection.

If anyone has anything better then I would like to hear your suggestions.

Code:
Private Sub ComboBox24_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
      If KeyCode = vbKeyUp Then KeyCode = vbNull
        If KeyCode = vbKeyDown Then KeyCode = vbNull
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,505
Messages
6,179,152
Members
452,891
Latest member
JUSTOUTOFMYREACH

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