Behavior when Userform [X] is pressed

wpryan

Well-known Member
Joined
May 26, 2009
Messages
534
Office Version
  1. 365
Platform
  1. Windows
Hello,
I have a userform that populates a cell with some text based on the data entered on the userform. I would like it such that when a person closes the form without entering any data by pressing the [X] button, some "default" text is entered. How can this be done?
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
You can use the QueryClose event in the Userform. So for example:

VBA Code:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If ActiveCell.Value = "" Then ActiveCell.Value = "Default text"
End Sub

Will add "Default Text" to the ActiveCell when (1) you press the UserForm close button; and (2) the ActiveCell is blank.

Hope that helps. Let me know if I've misunderstood the problem.
 
Upvote 0
Dan_W's code is the most simplest one.

Another example... You can modify as necessary... Below will add the "Default Text "to the Textbox on the user form (not sure how your data is passed from form to workbook. ) as well as pass it to the activecell....

VBA Code:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If ActiveCell.Value = "" And TextBox1.Value = "" Then
        ActiveCell.Value = "Default Text"
        TextBox1.Value = "Default Text"
    End If
    MsgBox "Nothing Entered. Default Text Inserted"
End Sub

Screenshot 2022-07-26 145704.png
 
Upvote 0
Solution

Forum statistics

Threads
1,214,593
Messages
6,120,435
Members
448,961
Latest member
nzskater

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