user3725366
user3725366

Reputation: 13

How to stop <Enter> moving to next field in Form (Excel VBA )

Am coding am Excel input form in VBA. I have a text box. I want to run a macro, and then get the focus to stay on/return to a text box after hitting enter from within that text box.

I've got the macro to run fine by using _keydown (code=13), but can't get focus to stay in the box. I've tried a setfocus at the end of the macro being called, but to no avail. I'm guessing that the code to move to the next field runs after the keydown detection.

I've searched online for the answer, but can't find anything.

Anyone know how to do this?

Thanks, Chris.

Upvotes: 1

Views: 3851

Answers (1)

Jzz
Jzz

Reputation: 739

After you catch the keycode, set the keycode to 0, so:

If KeyCode = 13 Then
    'do stuff
    KeyCode = 0
End If

Upvotes: 4

Related Questions