Spinbutton action

colinharwood

Active Member
Joined
Jul 27, 2002
Messages
426
Office Version
  1. 2019
Platform
  1. Windows
Hi I have a userform with 6 textbox's and when I press enter, the focus moves to the next textbox.
I have now added a spinbutton to increase or decrease the value of one of the textbox's.
My problem is that now the only way I can exit the spinbutton is by the tab key, not the enter key, is there a setting I need to change to make the spinbutton exit on pressing the enter key
Thanks
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Maybe something along these lines:
VBA Code:
Option Explicit

Private Sub UserForm_Activate()
    Dim ctl As Control, iTabIndx As Integer
    
    On Error Resume Next
    iTabIndx = SpinButton1.TabIndex
    For Each ctl In Me.Controls
        If Not ctl Is Me.SpinButton1 Then
            If ctl.TabStop Then
                If ctl.TabIndex = iTabIndx + 1 Then
                    SpinButton1.Tag = ctl.Name
                End If
                If Len(SpinButton1.Tag) = 0 Then
                    SpinButton1.Tag = Me.Controls(0).Name
                End If
            End If
        End If
    Next ctl
End Sub


Private Sub SpinButton1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)

    If KeyCode = vbKeyReturn Then Me.Controls(SpinButton1.Tag).SetFocus

End Sub
 
Upvote 0
Hi
I have tried your code, but I cannott get it to work, infact with the code, you cannott use the keyboard to increment or decrease the textbox value
Thanks for trying
 
Upvote 0
Hi All
Just for the record, I have continued to play around with this problem, and have found that the following code allows me to press the enter key, and move to the next textbox
VBA Code:
Private Sub SpinButton1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    Update.SetFocus

End Sub
 
Upvote 0
Hi All
Just for the record, I have continued to play around with this problem, and have found that the following code allows me to press the enter key, and move to the next textbox
VBA Code:
Private Sub SpinButton1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    Update.SetFocus

End Sub
That would move the focus to the next control when pressing any key not just the Enter key .

EDIT:
You never mentioned in your initial post that you were using the keyboard to increment or decrease the textbox value
 
Upvote 0
Thanks for pointing that out, I had not thought about the fact that any key would move the focus. As it is now, it performs in the way that I want it to, although others may not find this so. I also thought that the use of the keyboard arrows was a default setting for a spinbutton, Perhaps I am wrong.
 
Upvote 0
Thanks for pointing that out, I had not thought about the fact that any key would move the focus. As it is now, it performs in the way that I want it to, although others may not find this so. I also thought that the use of the keyboard arrows was a default setting for a spinbutton, Perhaps I am wrong.

Try just this :
VBA Code:
Private Sub SpinButton1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    If KeyCode = vbKeyReturn Then Update.SetFocus
End Sub

and yes you are correct. The keyboard arrows is a default setting for a spinbutton
 
Upvote 0

Forum statistics

Threads
1,214,974
Messages
6,122,536
Members
449,088
Latest member
RandomExceller01

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