Assign arrow keys to trigger command button click for a specific command button.

kbishop94

Active Member
Joined
Dec 5, 2016
Messages
476
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
What I'd like the code to do is to advance the userform frontwards (right arrow key) or backwards (left arrow key) like the userform does right now when the specific command buttons are currently clicked:

I'm just showing the code to advance to the next record on the spreadsheet ("cmdNext")... the other one (go to the previous record; "cmdPrevious") is the same except this code has '-1' instead)


VBA Code:
Private Sub cmdNext_Click()
Dim ctl
' FIRST CLEAR ALL CHECKBOXES & TEXTBOXES THAT CONTAINED DATA FROM PREVIOUS INCIDENT ID RECORD.
    For Each ctl In Me.Controls
        If TypeOf ctl Is MSForms.TextBox Then
            ctl.Text = ""
        ElseIf TypeOf ctl Is MSForms.ComboBox Then
            ctl.Object.value = ""
        ElseIf TypeOf ctl Is MSForms.CheckBox Then
            ctl.Object.value = False
        End If
    Next ctl
' NOW FIND THE NEXT VISIBLE ROW **BELOW** THE CURRENT INCIDENT ID RECORD & POPULATE THE USERFORM WITH THAT DATA
        If Not FoundCell Is Nothing Then
            Do
                Set FoundCell = FoundCell.Offset(1)
                Loop While FoundCell.EntireRow.Hidden = True
        Debug.Print FoundCell.Row
        GetRecord FoundCell.Row
    End If
End Sub

The red arrows in the pic below shows how it works right now... the above code is ran when the right green command button (smdNext) is clicked (the left one does the same except it goes backwards using -1 instead of 1). This will make the userform to clear its contents and then reload everything with the next record BELOW the current record (row) that is being viewed... So in this example, the userform is displaying the data from the row with "22-1245" in column A... clicking on the right green command button will advance it to the next record, "22-1246"

I want to modify the code so when the right arrow key is pressed it will run the cmdNext code, and when the left arrow is pressed, it will run the cmdPrevious code. How would I do that? Thanks!

ArrowKeys-4.jpg
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Does this help?


When you initialize the form, you can assign the {LEFT} and {RIGHT} keys to specific procedures. Be sure to reset them to default when unloading the form, of course.
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,632
Latest member
jladair

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