1 Attachment(s)
How to set focus back into the textbox after an error message?
I have a user form with several textboxes and when user inputs data , it gets stored on excel sheet. I am having trouble setting the cursor back into the textbox after an error message, while I am doing the data validation in my last command button called save. Although, the problem does not seem to appear, while I am doing the data validation in the exit events of the individual text boxes. The code is as seen below.
Code:
Code:
Private Sub CmdSave1_Click()
Dim row As Long
Dim c As Range
row = ActiveCell.row
For Each c In Range("A2:A" & Cells(Rows.Count, 1).End(xlUp).row)
'Call txt_BPName1_Exit(c)
If c.Value = txt_BPName1 Then
MsgBox " Duplicate Found.Please enter unique Base Product"
txt_BPName1.SetFocus '>>> the cursor does not return to textbox here.
'cancel = True
End If
Next
txt_SPName1.SetFocus
'Exit Sub
For Each c In Range("B2:B" & Cells(Rows.Count, 1).End(xlUp).row)
If c.Value = txt_SPName1 Then
MsgBox "Cell " & c.Address & " Duplicate sub Product Found."
'txt_SPName1.SetFocus
cancel = True
End If
Next
txt_loc1.SetFocus
'Exit Sub
I have attached the file here for reference. please check out Add_InventoryBP Userform for full code.
Thanks,
Kann