ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Help with SetFocus required please (https://www.excelbanter.com/excel-programming/399304-help-setfocus-required-please.html)

Jim[_70_]

Help with SetFocus required please
 
I have a combobox in a userform and wish to prevent the leaving the value
blank.
I have used the following code

If ComboBox1.listIndex = -1 Then
Msgbox "Please select a value"
Combobox1.SetFocus
End If

It doesn't work - the focus moves to the next control

Why is this ?



Jim[_70_]

Help with SetFocus required please
 
I should have specified that I have used the code in the Exit Procedure as
follows

Private Sub ComboBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)

If ComboBox1.listIndex = -1 Then
Msgbox "Please select a value"
ComboBox1.SetFocus
End If

End Sub


Regards & TIA



"Jim @Eclipse.co.uk" <JimBurton<NOSPAM wrote in message
...
I have a combobox in a userform and wish to prevent the leaving the value
blank.
I have used the following code

If ComboBox1.listIndex = -1 Then
Msgbox "Please select a value"
Combobox1.SetFocus
End If

It doesn't work - the focus moves to the next control

Why is this ?




Rick Rothstein \(MVP - VB\)

Help with SetFocus required please
 
Instead of ComboBox1.SetFocus, try using Cancel = True instead.

Rick


"Jim @Eclipse.co.uk" <JimBurton<NOSPAM wrote in message
...
I should have specified that I have used the code in the Exit Procedure as
follows

Private Sub ComboBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)

If ComboBox1.listIndex = -1 Then
Msgbox "Please select a value"
ComboBox1.SetFocus
End If

End Sub


Regards & TIA



"Jim @Eclipse.co.uk" <JimBurton<NOSPAM wrote in message
...
I have a combobox in a userform and wish to prevent the leaving the value
blank.
I have used the following code

If ComboBox1.listIndex = -1 Then
Msgbox "Please select a value"
Combobox1.SetFocus
End If

It doesn't work - the focus moves to the next control

Why is this ?





Jim[_70_]

Help with SetFocus required please
 
Thank you Rick - works just as I need

"Rick Rothstein (MVP - VB)" wrote in
message ...
Instead of ComboBox1.SetFocus, try using Cancel = True instead.

Rick


"Jim @Eclipse.co.uk" <JimBurton<NOSPAM wrote in message
...
I should have specified that I have used the code in the Exit Procedure as
follows

Private Sub ComboBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)

If ComboBox1.listIndex = -1 Then
Msgbox "Please select a value"
ComboBox1.SetFocus
End If

End Sub


Regards & TIA



"Jim @Eclipse.co.uk" <JimBurton<NOSPAM wrote in message
...
I have a combobox in a userform and wish to prevent the leaving the value
blank.
I have used the following code

If ComboBox1.listIndex = -1 Then
Msgbox "Please select a value"
Combobox1.SetFocus
End If

It doesn't work - the focus moves to the next control

Why is this ?








All times are GMT +1. The time now is 12:33 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com