+ Reply to Thread
Results 1 to 9 of 9

Adding Code To The Action of A Userform Red Corner X

  1. #1
    Forum Contributor
    Join Date
    06-15-2009
    Location
    Ontario, canada
    MS-Off Ver
    Office 365
    Posts
    371

    Adding Code To The Action of A Userform Red Corner X

    I have an open userform. I don't want it's users to exit the form by instinctively clicking the red top right corner X (I do have a preferred exit button) as this, combined with closing the workbook itself incorrectly may result in data loss.\

    I was hoping I could use code to purpose it's function to save the workbook before closing the form, that way the workbook will be saved on closing the userform accidently by the red X.

    Here is the code I researched but I can't seem to get it to work. The code is never triggered to be executed.

    Please Login or Register  to view this content.

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,289

    Re: Adding Code To The Action of A Userform Red Corner X

    That's the correct event to intercept the close button. Did you put it into the userform code module?

    If you already have an Exit button, you could just call its click event from the Queryclose event.
    Remember what the dormouse said
    Feed your head

  3. #3
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,126

    Re: Adding Code To The Action of A Userform Red Corner X

    Alternatively, you could disable the X like
    Please Login or Register  to view this content.

  4. #4
    Forum Contributor
    Join Date
    06-15-2009
    Location
    Ontario, canada
    MS-Off Ver
    Office 365
    Posts
    371

    Re: Adding Code To The Action of A Userform Red Corner X

    Perfect Rory! Thank you. I had it in the wrong userform module!
    Fluff, I like that idea to! Along this idea Fluff, I am using something similar (disabling the X button) at the workbook level to prevent an 'illegal exit'.

    Please Login or Register  to view this content.
    The problem I'm encountering though is with the EXIT button code that closes and saves that workbook.

    Please Login or Register  to view this content.
    It can't seem to do this (save then close) because the 'Workbook_BeforeClose' event triggers preventing it. Has anyone any suggestions on how to overcome this?
    Last edited by Jenn68; 01-24-2019 at 12:46 PM.

  5. #5
    Forum Expert nigelog's Avatar
    Join Date
    12-14-2007
    Location
    Cork, Ireland
    MS-Off Ver
    Office 365 Windows 10
    Posts
    2,286

    Re: Adding Code To The Action of A Userform Red Corner X

    Have you tried the before close event??

  6. #6
    Forum Contributor
    Join Date
    06-15-2009
    Location
    Ontario, canada
    MS-Off Ver
    Office 365
    Posts
    371

    Re: Adding Code To The Action of A Userform Red Corner X

    Hi nigelog ... I'm sorry, I don't understand your solution. I am using the before_close event, but the problem arises when I run code to close (with saving) that workbook with the before_close event associated to it.

  7. #7
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,126

    Re: Adding Code To The Action of A Userform Red Corner X

    Untested, but try
    Please Login or Register  to view this content.

  8. #8
    Forum Contributor
    Join Date
    06-15-2009
    Location
    Ontario, canada
    MS-Off Ver
    Office 365
    Posts
    371

    Re: Adding Code To The Action of A Userform Red Corner X

    Thank you so much Fluff, that worked very well. I did notice though the
    Please Login or Register  to view this content.
    lines aren't executed (for obvious reasons). This won't affect Excel in general will it? It's kind odd to see excel as just one blank grey canvas (no blank or other worksheet).

  9. #9
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,289

    Re: Adding Code To The Action of A Userform Red Corner X

    Workbook_BeforeClose doesn't have a CloseMode argument, so it looks like you're just using an undeclared variable (which will always be 0). You should declare a variable and set that appropriately when you want your code to be able to close the workbook/application.

+ 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 button action - clear,copy paste and print crashes. Please help with code
    By intex in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-05-2014, 12:16 PM
  2. Adding a sort to userform code
    By excelloser in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-21-2012, 03:30 AM
  3. adding a formula to a column of cells with incremental value (like right corner drag)
    By KAPearson in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 05-22-2012, 02:24 PM
  4. Userform in upper right corner
    By stoven in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-21-2012, 12:22 PM
  5. Adding Code to an Existing UserForm with VBA
    By JimmyA in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-08-2010, 12:02 PM
  6. Adding code to a userform at runtime
    By mikerickson in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-27-2007, 06:19 PM
  7. RE: insert 2 colours in a single cell,corner to corner
    By Toppers in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 06-23-2006, 03:50 AM

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