End a userform on commandbutton click VBA

Sweedler

Board Regular
Joined
Nov 13, 2020
Messages
114
Office Version
  1. 2016
Platform
  1. Windows
  2. MacOS
Hi

So I have a Userform I am using to fill in data on a sheet. I have put in place extra Macros to keep me from leaving any spaces blank (see below):

Private Sub listedbox_Exit(ByVal cancel As MSForms.ReturnBoolean)

If Trim(listedbox.Value) = "" And Me.Visible Then
MsgBox "Listed or Unlisted"
cancel = True
investmentnew.listedbox.SetFocus
listedbox.BackColor = vbYellow
Else
listedbox.BackColor = vbWhite

End If
End Sub

The problem comes when I try to exit/cancel the user form. These safeguards don't let me exit the userform as there are still blanks.

Any suggestions for how I can skip these Macros when clicking cancel or a way to go around somehow. I have tried everything I can think of.
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Try this.
The UserForm_QueryClose event fires when you close the form, so you can change the value of the comingout variable to true.
If the comingout variable is true then it means that you are going to terminate the form.

VBA Code:
Dim comingout As Boolean    'to the top of all the code

Private Sub listedbox_Exit(ByVal Cancel As MSForms.ReturnBoolean)
  If comingout Then End
  If Trim(listedbox.Value) = "" And Me.Visible Then
    MsgBox "Listed or Unlisted"
    Cancel = True
    investmentnew.listedbox.SetFocus
    listedbox.BackColor = vbYellow
  Else
    listedbox.BackColor = vbWhite
  End If
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
  comingout = True
End Sub
 
Upvote 0
In my opinion you shouldn't use this approach - in my experience it will annoy your users. You should have a Submit/Save type button that performs any necessary checks before actually saving the data to the sheet (you could still use these events to highlight invalid controls if you like).
 
Upvote 0

Forum statistics

Threads
1,214,923
Messages
6,122,283
Members
449,075
Latest member
staticfluids

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