Hi... I am fairly new to VBA programming in EXCEL. So my apologies in advance. I have a USERFORM with several CHECKBOXs and one TEXTBOX. When the user clicks a checkbox the textbox appears next to it for the user to enter dollar amounts.

I am trying to use the ENTER KEY from the TEXTBOX to set the TEXTBOX.VISABLE to FALSE. But, I cannot get it working. I have tried to use KeyPress, KeyDown, KeyUp and Exit to no avail. When I check for the KeyAscii or KeyCode it is blank. I anly see the numbers that were entered in the TEXTBOX.

Do you know how I can check for the enter key from within the TEXTBOX? Thank you in advance.

Dani AI

Generated

This thread mixes Excel VBA and .NET answers. is using an Excel UserForm; posted a WinForms snippet and 's KeyPreview note is a WinForms tip. For an Excel UserForm you should use the TextBox KeyPress/KeyDown events inside the UserForm code and test for Enter (vbKeyReturn = 13). The VBA approach is slightly different from the .NET example already shown.

A compact, practical VBA KeyPress example (UserForm TextBox) that formats numeric input, hides the box on Enter and swallows the keystroke:

Private Sub TextBox1_KeyPress(ByVal KeyAscii As Integer)
    If KeyAscii = vbKeyReturn Then
        If Len(Trim(TextBox1.Value)) > 0 And IsNumeric(TextBox1.Value) Then
            TextBox1.Value = FormatCurrency(CDbl(TextBox1.Value))
        End If
        TextBox1.Visible = False
        KeyAscii = 0     ' prevent Enter from triggering other controls
        Me.CheckBox1.SetFocus
    End If
End Sub

Troubleshooting notes tied to the thread:

  • If the textbox is an ActiveX control on a worksheet, put the same event in the sheet module (not the UserForm).
  • If the TextBox is MultiLine = True, Enter inserts a newline; set MultiLine = False or use KeyDown/KeyUp to detect vbKeyReturn.
  • Use KeyAscii = 0 to stop Enter from firing a default CommandButton.
  • If multiple checkboxes reuse one textbox, store which checkbox opened it (a module-level variable) so you can return focus or update the correct checkbox after hiding.

If the environment really is VB.NET (WinForms), 's approach is correct; the VBA code above is for Excel UserForms.

Yes you can check just make sure that key preview property of that form will need to be enabled. if it is enabled then you will easily gate the value of the textboxes....

Private Sub TextBox1_[B]KeyPress[/B](ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        [B]If e.KeyChar = Chr(13) Then TextBox1.Visible = False[/B]
    End Sub

Thank you so much for your help. Your suggestion was very helpful.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.