Set focus back to seme TexBox

Sahak

Well-known Member
Joined
Nov 10, 2006
Messages
1,012
Office Version
  1. 2016
  2. 2013
  3. 2011
  4. 2010
  5. 2007
Hi all,

On UserForm I have textBox were user should enter school name, I would like, if the name already exists, to get message that the name exists & Focus stays in same TextBox (not move to the next control)
Thank you in advance.
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Use the TextBox1_Exit event procedure. Maybe something like this. (you didn't explain how to test if the name exists with your data. The IF statement is psudo-code)

Code:
[color=darkblue]Private[/color] [color=darkblue]Sub[/color] TextBox1_Exit([color=darkblue]ByVal[/color] Cancel [color=darkblue]As[/color] MSForms.ReturnBoolean)
    [color=darkblue]If[/color] Name_Exists [color=darkblue]Then[/color]
        MsgBox TextBox1.Text, vbExclamation, "Name Exists"
        TextBox1.Text = ""
        Cancel = [color=darkblue]True[/color] [color=green]'Don't exit the textbox[/color]
    [color=darkblue]End[/color] [color=darkblue]If[/color]
End [color=darkblue]Sub[/color]
 
Upvote 0
Perhaps something like this ...

Amend to match your own names and ranges
- textbox name = tb_School
- list of schools = Sheets("Schools").Columns("A")

Code:
Private Sub tb_School_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Dim List As Range, Sch As Control
    Set List = Sheets("Schools").Columns("A")
    Set Sch = tb_School
    If Sch.Value = "" Then Exit Sub
    
    If WorksheetFunction.CountIf(List, Sch.Text) > 0 Then
        MsgBox Sch.Text, vbExclamation, "DUPLICATE"
        Cancel = True
        Sch.Text = ""
    End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,207
Members
452,618
Latest member
Tam84

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