+ Reply to Thread
Results 1 to 14 of 14

Check before exiting SHEET1 if MsgBox is active or not and stop exit if MsgBox is active

  1. #1
    Forum Contributor
    Join Date
    05-04-2015
    Location
    Colombo, Sri Lanka
    MS-Off Ver
    2010
    Posts
    129

    Check before exiting SHEET1 if MsgBox is active or not and stop exit if MsgBox is active

    Hello Friends!,

    I got this workbook consisting of about 10 sheets (SHEET1 to SHEET10). SHEET1 is data entry. Other sheets fill in automatically based on data entered in SHEET1. SHEET1 got these MsgBoxes programmed to pop up just in case that a wrong entry is chosen or inserted something in error etc.

    Even with a MsgBox being triggered, I can exit SHEET1 and click on SHEET2and this action does not stop me with a pop up saying that there is an error in SHEET1.

    This is what I want. I need a macro to check all the MsgBoxes (before exiting the SHEET1) and deny permission to move if there is an error.

    Will be pleased to hear and thank you for same.

    Best wishes to all,
    Rizvi Sameem

  2. #2
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Check before exiting SHEET1 if MsgBox is active or not and stop exit if MsgBox is acti

    Hi there,

    Something like the following code entered into the VBA CodeModules of the worksheets you want to monitor might do what you need:

    Please Login or Register  to view this content.

    Hope this helps - please let me know how you get on.

    Regards,

    Greg M

  3. #3
    Forum Contributor
    Join Date
    05-04-2015
    Location
    Colombo, Sri Lanka
    MS-Off Ver
    2010
    Posts
    129

    Re: Check before exiting SHEET1 if MsgBox is active or not and stop exit if MsgBox is acti

    Hi Greg,

    Thank you very much. There is a "Run-time error '5':" appeearing with a message "Invalid procedure call or argument".

    Regards
    Rizvi

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Check before exiting SHEET1 if MsgBox is active or not and stop exit if MsgBox is acti

    Rizvi

    How are you currently displaying the message boxes?
    If posting code please use code tags, see here.

  5. #5
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Check before exiting SHEET1 if MsgBox is active or not and stop exit if MsgBox is acti

    Hi again,

    In the absence of a sample workbook I assumed that what you describe as "MsgBoxes" were Shapes named "shpMessageBox", and wrote the code accordingly.

    If your "MsgBoxes" have some other form or name, the code will obviously fail.

    When submitting a question here, you should ALWAYS include a sample workbook, otherwise proposed solutions can be based only on assumptions or suppositions.

    Regards,

    Greg M

  6. #6
    Forum Contributor
    Join Date
    05-04-2015
    Location
    Colombo, Sri Lanka
    MS-Off Ver
    2010
    Posts
    129

    Re: Check before exiting SHEET1 if MsgBox is active or not and stop exit if MsgBox is acti

    Hello Norie and Greg,

    Please accept my apology for not replyng you promptly. Here is the code and I got about 40 such codes running;

    Please Login or Register  to view this content.
    Regards
    Rizvi

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Check before exiting SHEET1 if MsgBox is active or not and stop exit if MsgBox is acti

    Where is this code located?

  8. #8
    Forum Contributor
    Join Date
    05-04-2015
    Location
    Colombo, Sri Lanka
    MS-Off Ver
    2010
    Posts
    129

    Re: Check before exiting SHEET1 if MsgBox is active or not and stop exit if MsgBox is acti

    under; Private Sub Worksheet_Change(ByVal Target As Range)

  9. #9
    Forum Contributor
    Join Date
    05-04-2015
    Location
    Colombo, Sri Lanka
    MS-Off Ver
    2010
    Posts
    129

    Re: Check before exiting SHEET1 if MsgBox is active or not and stop exit if MsgBox is acti

    This is the code;

    Please Login or Register  to view this content.
    Last edited by RIZVI; 05-29-2021 at 11:35 PM.

  10. #10
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Check before exiting SHEET1 if MsgBox is active or not and stop exit if MsgBox is acti

    Hi again,

    Thanks for that additional information. See if the following code does what you need - it should be entered in the VBA CodeModules of the worksheets you wish to monitor:

    Please Login or Register  to view this content.

    Hope this helps.

    Regards,

    Greg M

  11. #11
    Forum Contributor
    Join Date
    05-04-2015
    Location
    Colombo, Sri Lanka
    MS-Off Ver
    2010
    Posts
    129

    Re: Check before exiting SHEET1 if MsgBox is active or not and stop exit if MsgBox is acti

    Hi Greg,

    This is what I want, that one cannot leave SHEET1 unless the error input is corrected. Perfect! BUT;

    As said,there are about 40 MsgBox codes entered;

    Please Login or Register  to view this content.
    So, for all 40 such codes I have to enter ".Activate" after the IF line. Actually took 5 codes, pasted under "Private Sub Worksheet_Deactivate()" tested by placing 5 such ".Active" codes and works perfectly. But the issue is each and every time I create a code and place it under my usual code "Private Sub Worksheet_Change(ByVal Target As Range)", I also have to create a duplicate and paste it under "Private Sub Worksheet_Deactivate()" because "Private Sub Worksheet_Change(ByVal Target As Range)" shows the MsgBox immediately where one can correct the mistake on the spot whilst "Private Sub Worksheet_Deactivate()" shows the MsgBox only when exiting the SHEET1.

    What I acuallyy need is for the MsgBox to pop-up immediately if a wrong item is pressed or selected so that one can correct it immediately (Private Sub Worksheet_Change(ByVal Target As Range)) and I also need a separate code such as yours (Private Sub Worksheet_Deactivate()) to check in general if any such MsgBox in "Private Sub Worksheet_Change(ByVal Target As Range)" are triggered before exiting the SHEET1.

    Thanks again Greg!

    Best wishes
    Rizvi

  12. #12
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Check before exiting SHEET1 if MsgBox is active or not and stop exit if MsgBox is acti

    Hi again,

    As I said before, it is MUCH easier to suggest a solution when the entire scenario is visible from the start, instead of being drip-fed additional information when the suggested solution does not meet the (hitherto unstated!) requirements

    See if the following code does what you need:

    Please Login or Register  to view this content.

    Regards,

    Greg M

  13. #13
    Forum Contributor
    Join Date
    05-04-2015
    Location
    Colombo, Sri Lanka
    MS-Off Ver
    2010
    Posts
    129

    Re: Check before exiting SHEET1 if MsgBox is active or not and stop exit if MsgBox is acti

    buddy,

    It's perfect!, just what I needed. Tested on just 2 codes and works like a charm.
    Thanks Greg, appreciated your effort in assisting.

    Best wishes
    Rizvi

  14. #14
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Check before exiting SHEET1 if MsgBox is active or not and stop exit if MsgBox is acti

    Hi again,

    Many thanks for your feedback and also for the Reputation increase - much appreciated!

    We got there in the end!

    You're welcome - glad I was able to help.

    Best regards,

    Greg M

+ 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] MSGbox if cel C17 from active sheet is empty
    By FvdF in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-09-2021, 10:46 AM
  2. Replies: 1
    Last Post: 07-31-2015, 07:06 PM
  3. How to get a MsgBox with no active button
    By Elippam80 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-22-2015, 02:39 PM
  4. VBA if active cell blank than msgbox to remind
    By dekueb in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-14-2015, 10:48 AM
  5. Import data from workbook into Active Sheet, and Allow manipulation of WB around MsgBox
    By DaiyannaGrae in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-05-2013, 12:54 PM
  6. [SOLVED] VBA. Check office user name, if certain format is not met then MsgBox and exit workbook
    By rain4u in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-09-2012, 06:58 AM
  7. MsgBox if the Active Cell is not within a preferred Range
    By Winon in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-03-2011, 05:52 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