+ Reply to Thread
Results 1 to 5 of 5

Preserve values from a userform when red X button is clicked

Hybrid View

  1. #1
    Registered User
    Join Date
    05-12-2021
    Location
    México
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (16.0.13530.20054)
    Posts
    17

    Preserve values from a userform when red X button is clicked

    Dear experts:

    Hope you are doing fine!

    I'm just learning to program on VBA and I'm trying to "enable" a dialog that pops up when the red X on the top of a userform is clicked. This dialog must ask to the user if the values already provided must be saved or not.

    I think it is related to the QueryClose event but I'm not sure how it should be composed.

    I have Option buttons and text boxes.

    Hope you can help me, thanks in advance! Have a great day!

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: Preserve values from a userform when red X button is clicked

    You need to Cancel the event and just hide the form.
    Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
        Cancel = True
        Me.Hide
    End Sub

  3. #3
    Registered User
    Join Date
    05-12-2021
    Location
    México
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (16.0.13530.20054)
    Posts
    17

    Re: Preserve values from a userform when red X button is clicked

    Thanks Jindon, this is not quite what I need, but I appreciate the help

  4. #4
    Forum Contributor
    Join Date
    05-09-2021
    Location
    Sweden
    MS-Off Ver
    Office 365
    Posts
    148

    Re: Preserve values from a userform when red X button is clicked

    Hello Magmarinita,

    Just place a messagebox with a question and listen to the response and take action from there...

    Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
        
        If MsgBox("Save data?", vbYesNo, "Save data?") = vbYes Then
            'save thedata somewhere... See below...
            
            MsgBox "Data has been saved.", vbOKOnly, "Success"
        End If
        
        Me.Hide
        
    End Sub
    Now it depends what you mean with save the data...
    If you are going to "save it" in the sense that you want to use it directly in your code then you could either just assign the data to a global variable, or you can access the form data later by referring to theuserform...
    ' In the form code
    somevariable = me.Textbox1.value
    
    'outside the form code
    somevariable = someuserform.Textbox1.value
    if you are going to save it temporarily somewhere to pick it up later, you could just save it in a cell somewhere
    someworksheet.cells(somerow,somecolumn) = me.Textbox1.value
    if you want to save the data, like statistics, you could save it in a listobject, a table...
    set somelistrow = somelistobject.listrows.add
    somelistobject.listcolumn("somecolumnname").databodyrange(somelistrow.index) = me.Textbox1.value
    Hopefully this guide you in the right direction.
    Last edited by MrChulo; 06-16-2021 at 05:07 AM.

  5. #5
    Registered User
    Join Date
    05-12-2021
    Location
    México
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (16.0.13530.20054)
    Posts
    17

    Re: Preserve values from a userform when red X button is clicked

    Thank you so much, this is exactly what I was looking for, thank you so much!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Userform Macro to get the name of the button clicked
    By meabrams in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-02-2016, 03:19 AM
  2. [SOLVED] Capture name of a userform button after it has been clicked and opened another form
    By Jerbinator in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-31-2016, 10:09 AM
  3. Calling the name of the clicked button in a userform?
    By ckreitel in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-25-2016, 03:42 PM
  4. [SOLVED] Exit Userform After Button Clicked
    By Building in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-12-2015, 04:05 PM
  5. [SOLVED] Userform Cancel button when clicked still inputs data
    By Mr_Bill in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-27-2014, 01:04 PM
  6. vba Userform help - displaying a description based on which button is clicked
    By Bandicoot in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-21-2013, 07:39 AM
  7. Knowing which button was clicked on UserForm?
    By Riorin in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-07-2008, 04:15 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1