Enter Key Press Disable If TextBox is Empty

AsifShah

Board Regular
Joined
Feb 12, 2021
Messages
70
Office Version
  1. 2016
  2. 2013
Platform
  1. Windows
Hello Everyone,

If TextBox6 is empty then Enter Key Press will disable and TextBox6 set focus curser did not move to any other TextBox
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
why bother with that if a user can tab out or use mouse to click out? Wouldn't you need to cover all situations? If yes, you could use the control's exit event and cancel it if the control has no value.
 
Upvote 0
why bother with that if a user can tab out or use mouse to click out? Wouldn't you need to cover all situations? If yes, you could use the control's exit event and cancel it if the control has no value.
There are 10 textbox in userform and one command button.
Some boxes are empty and need to save data so I hold enter key until it reaches the command button and command button have set focus command (TextBox1.setfocuse).
Textbox1 is automatically selected, sometimes the mouse cursor reaches textbox3 and textbox4 when the enter key is pressed for a long time.That's why I want mouse cursor to be set focus in the taxbox1 until taxbox1 is empty.
 
Upvote 0
That doesn't answer my question about how this stops anyone from using mouse or tab key.
I guess try this in the keydown event for the textbox then

If KeyCode = 13 Then KeyCode = 0
 
Upvote 0
Solution
Paste the following in the UserForm:

VBA Code:
Option Explicit

Private Sub CommandButton1_Click()

Dim ctrl As Control      ' CREATE A CONTROL OBJECT.

    ' LOOP THROUGH EACH CONTROL, CHECK IF THE CONTROL IS A TEXTBOX.
    For Each ctrl In Me.Controls
        If TypeName(ctrl) = "TextBox" Then
            If ctrl.Value = "" Then
                ctrl.SetFocus
                MsgBox "All Textboxes must have data !", vbCritical, "Error"
                Exit Sub
            End If
        End If
    Next ctrl
    
End Sub
 
Upvote 0
Paste the following in the UserForm:

VBA Code:
Option Explicit

Private Sub CommandButton1_Click()

Dim ctrl As Control      ' CREATE A CONTROL OBJECT.

    ' LOOP THROUGH EACH CONTROL, CHECK IF THE CONTROL IS A TEXTBOX.
    For Each ctrl In Me.Controls
        If TypeName(ctrl) = "TextBox" Then
            If ctrl.Value = "" Then
                ctrl.SetFocus
                MsgBox "All Textboxes must have data !", vbCritical, "Error"
                Exit Sub
            End If
        End If
    Next ctrl
   
End Sub
Thanks for your time dear .. But i was try this.. it dose not work like i want.
 
Upvote 0
That doesn't answer my question about how this stops anyone from using mouse or tab key.
I guess try this in the keydown event for the textbox then

If KeyCode = 13 Then KeyCode = 0
i was just add
VBA Code:
If TextBox6.Value = "" Then
If KeyCode = 13 Then KeyCode = 0
End If
and its work perfect and thank you for giving me time from your precious time.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,537
Messages
6,125,393
Members
449,222
Latest member
taner zz

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