Having Issues with TextBox in Userform Enter function

86ms3

New Member
Joined
Nov 16, 2017
Messages
14
I have a userform where I have a textbox. Everytime the user types in a pallet number then hits the 'enter' key, I want it to save the value to a cell defined as the last row and column. Then I want the textbox to clear and the focus to stay in the text box when the user presses enter so that they can enter in another value and so on. But the problem is when the user hits enter it will enter in the data into the correct cell, but the userform will lose focus and it will just tab over to the next item in the tabindex.

Maybe I'm going about this the wrong way, but I can't seem to figure out this issue.



Code:
Private Sub TextBoxIndividualPallet_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)

    If KeyCode = vbKeyReturn Then
    LastHold = Sheets("Holds").Range("A" & Rows.Count).End(xlUp).Row
        With Sheets("Holds")
            LastCol = .Cells(LastHold, .Columns.Count).End(xlToLeft).Column
        End With
        Cells(LastHold, LastCol + 1) = "U" & TextBoxIndividualPallet.Value
        TextBoxIndividualPallet.Value = ""
        TextBoxIndividualPallet.SetFocus
    End If
End Sub
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Ok found a fix, finally. I don't quite understand it, or how, but it works.


Code:
Private Sub TextBoxIndividualPallet_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)

    If KeyCode = 13 Or KeyCode = 9 Then
    KeyCode = 0
    LastHold = Sheets("Holds").Range("A" & Rows.Count).End(xlUp).Row
        With Sheets("Holds")
            LastCol = .Cells(LastHold, .Columns.Count).End(xlToLeft).Column
        End With
        Cells(LastHold, LastCol + 1) = "U" & TextBoxIndividualPallet.Value
       
        TextBoxIndividualPallet.Value = ""
        TextBoxIndividualPallet.SetFocus
    End If
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,193
Messages
6,123,566
Members
449,108
Latest member
rache47

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