set focus error

depcdivr

Active Member
Joined
Jan 21, 2008
Messages
349
Office Version
  1. 365
Platform
  1. Windows
I have a user form that is having an issue with the set focus. Here is some of the code that I am using.




Code:
Private Sub PartNumber_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
 
If Me.done.Value <> True Then
If KeyCode = vbKeyReturn Then
 
' whole bunch of code deleted from here
 
end if 
end if
 
 
Me.PartNumber.SetFocus
End Sub
 
 
Private Sub PartNumber_Change()
 
' Even more code deleted from here
 
Me.PartNumber.SetFocus
End Sub

The issue that I am having is that when I hit the enter key after typing in a Partnumber the focus always goes to the next textbox. Even though I end the code by using the partnumber.setfocus command. In all of the deleted code I never set the focus elsewhere so I am confused as to why this is happening. Thank you for your help.
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Hi,

To my sense it is quite logic that the cursor moves on when you press "enter" or "returnkey" or "tab".

What would you like to happen when you press "enter"?

kind regards,
Erik
 
Upvote 0
When I press enter I want the userform to update a bunch of information but I want the focus to remain on the part number cell so that the user can enter a different part number without having to use the mouse to re-select the part number textbox.
 
Upvote 0
No the text box will not be erased at the end of the code.
the part number is a complicated number (15+ digits) and I want the user to be able to modify it if the description generated is not correct.
 
Upvote 0
this works for me

properties of your textbox
MultiLine = True
EnterKeyBehavior = True

add one line in your code
Code:
If KeyCode = vbKeyReturn Then
KeyCode = 0
' whole bunch of code deleted from here
End If
 
Upvote 0

Forum statistics

Threads
1,213,524
Messages
6,114,117
Members
448,549
Latest member
brianhfield

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