New Member
May 28, 2021
i have 3 checkbox,1 listbox and 3 textbox with values as 10. Iam adding some values based on check box1.value=true (i.e) AA,ABand ACto the listbox .
Iam adding some values based on check box2.value=true (i.e) BA,BBand BCto the same listbox .
Iam adding some values based on check box3.value=true (i.e) CA,CBand CCto the listbox .
if I select an item in the listbox(AA,AB,AC) and if the textbox1.text is >10 or null then I have to set the focus(text1.setfocus) in the listbox_change event
Private Sub ListBox1_Change()
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) Then
if listbox1.list(i)="AA" then
If (CheckBox1.Value = True) and CInt(TextBox1.Text) >= 10 Then
TextBox1.SetFocus
End If
end if
if listbox1.list(i)="BA" then
If (CheckBox2.Value = True) and CInt(TextBox2.Text) >= 10 Then
TextBox2.SetFocus
End If
end if
end if
next I
end sub
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Cancel = False
If (CheckBox1.Value = True) Then
If (CInt(TextBox1.Text) >= 10 Or TextBox1.Text = Null) = True Then
MsgBox "PLEASE CHK THE value(<10) "
Cancel = True
TextBox1.SetFocus
Else
Cancel = False
Exit Sub
End If
End If
End Sub
in this code when I select AA in the Listbox1 I am getting the msg MsgBox "PLEASE CHK THE value(<10) " and the selected item in the listbox gets deselected and the focus goes to textbox1.this happens for the first chkbox other chkbox and txtbox works fine.
Pls Help
Power Query
Power Pivot
Xtreme Pivot Tables
Excel for Decision Making
Excel for Finance
Excel Analysis Toolpak
Power BI
Excel
Word
Outlook
Excel Expert
Excel Customer Service
PowerPoint
November 8, 2013
Hi Raj,
At the top of the form module, add this variable:
Private EnableFormEvents as Boolean
Changes to:
Private Sub ListBox1_Change()
EnableFormEvents=False
' your code
EnableFormEvents=True
End Sub
Changes to:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
if EnableFormEvents=False then Exit Sub
'your code
End sub
Basically, when you set focus on a textbox and the focus was on the other textbox, the form needs to Exit from one textbox to focus on the other box, so you need to disable form events.
1 Guest(s)