Check two different statements on the Exit Routine

Pacman52

Active Member
Joined
Jan 29, 2009
Messages
415
Office Version
  1. 365
Platform
  1. Windows
Despite hours of searching and trying loads of different ways to resolve I have finally accepted its time to ask for help,

So my question is how do I write the exit sub to include the 2nd bit of code pasted below?

Many thanks for any advice or solutions offered. Paul


VBA Code:
Private Sub Textbox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)

Dim Result As VbMsgBoxResult

If ckBTextbox1.Value = True And Trim(Textbox1.Value) = "" Then
Textbox1.BackColor = RGB(255, 0, 0)

Result = MsgBox("You've ticked this box but not entered the a name." & vbCrLf & vbCrLf & _
"Click 'Yes' to enter the name or 'No' to un-tick.", vbInformation + vbYesNo, "Name Required:")

If Result = vbYes Then

Cancel = True
Textbox1.BackColor = RGB(204, 255, 255)

Else

ckBTextbox1.Value = False
Textbox1.BackColor = RGB(255, 255, 255)
Textbox1Desc.Enabled = False

End If
End If
End If

End Sub

VBA Code:
If ckBTextbox1.Value = True And Textbox1.Value > "" Then
Textbox1Desc.SetFocus
Textbox1Desc.BackColor = RGB(204, 255, 255)

End If
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
I don't understand what you want. There is no "exit sub" in that code so no idea where you'd want to exit nor under what condition. If both conditions in the first If block are not true, the entire If block is not executed so why can't you just add the second test? Or use a Select Case block? Maybe you should describe what you want to have happen.

BTW, when code is not indented it can be difficult to grasp what is supposed to happen when you're not the one who wrote it. Also, not that it really matters, but you seldom need to use .Value as it is the default property in most cases.
VBA Code:
Private Sub Textbox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim Result As VbMsgBoxResult

If ckBTextbox1.Value = True And Trim(Textbox1.Value) = "" Then
    Textbox1.BackColor = RGB(255, 0, 0)
    Result = MsgBox("You've ticked this box but not entered the a name." & vbCrLf & vbCrLf & _
            "Click 'Yes' to enter the name or 'No' to un-tick.", vbInformation + vbYesNo, "Name Required:")
    If Result = vbYes Then
        Cancel = True
        Textbox1.BackColor = RGB(204, 255, 255)
    Else
        ckBTextbox1.Value = False
        Textbox1.BackColor = RGB(255, 255, 255)
        Textbox1Desc.Enabled = False
    End If
End If

If ckBTextbox1.Value = True And Textbox1.Value > "" Then
    Textbox1Desc.SetFocus
    Textbox1Desc.BackColor = RGB(204, 255, 255)
End If
 
Upvote 0
Thanks for the reply. Sorry about not indenting the code properly on the post or really explaining what I actually wanted to achieve, The post was written after many hours of frustration trying to resolve this and other problems on the worksheet.

I've decided to simplify the form now but for reference what I was trying to do was have both the sets of code I pasted in my original post combined so that it all ran together on the exit routine.
 
Upvote 0
I get what you mean by "the exit sub" now. Exit Sub is an actual and often used command line in all sorts of procedures. You're referring to the Exit event for the control. Unfortunately I am non the wiser because repeating what you were trying to do is not really the same thing as revealing what is going on as the user is using the form. Anyway, it appears you have come up with a solution. The way I posted it does seem to answer how to combine the two parts, as long as all of it is between the Sub and End Sub lines.
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,178
Members
452,615
Latest member
bogeys2birdies

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