Share via

How do I get CommandButton to run code when I hit Enter, but skip to next CommandButton when I hit tab?

Anonymous
2024-01-21T05:18:08+00:00

Hi MS Community.

On my simple Userform, I have some TextBoxes, ComboBoxes and 2 CommandButtons.

CommandButton1 is for writing data to the Sheet, and CommandButton2 is to Exit the routine without saving.

Tab key or Enter key moves through input boxes in order (I've set this with TabIndex). I prefer to use Tab key to navigate through these input boxes, but Enter key does the same job.

When I get to the CommandButton1 (to write), I hit Enter and it writes as intended. Hitting Tab key (of course) moves to the next CommandButton2 without writing data.

If I hit Enter again, it writes data to the Sheet a second time (in the next row). I want to avoid accidentally hitting Enter again and writing the data again on the next row of the Sheet.

Is there a way for me to have it skip to the next CommandButton2 after Entering the first time so data can't inadvertently be duplicated?

I'm guessing there may be a Property for CommandButton1 which will move it to next TabIndex if Enter key is used, but I can't find a way to do this.

Thanks in advance.

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Andreas Killer 144.1K Reputation points Volunteer Moderator
2024-01-21T07:55:09+00:00

I'm guessing there may be a Property for CommandButton1 which will move it to next TabIndex if Enter key is used, but I can't find a way to do this.

Private Sub CommandButton1_Click()
'Your code here
Me.TextBox1.SetFocus
End Sub

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2024-01-21T08:26:22+00:00

    Thanks so much, Andreas Killer. Much appreciated.

    I'd seen something like this but didn't know how to use it properly.

    Cheers.

    Was this answer helpful?

    0 comments No comments