+ Reply to Thread
Results 1 to 13 of 13

Can we prevent Save option availability if it does not meet certain criteria?

  1. #1
    Registered User
    Join Date
    12-10-2018
    Location
    Chennai
    MS-Off Ver
    Excel 2016
    Posts
    3

    Can we prevent Save option availability if it does not meet certain criteria?

    Hi everyone,


    I have situation where I should not allow users to click Save unless they meet certain criteria ( Eg: if excel contains Red coloured cells in any Sheet, It should not be saved).

    Can I do this?

    Ps: I am using Excel 2016

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,481

    Re: Can we prevent Save option availability if it does not meet certain criteria?

    Attaching a sample workbook enables others to work on your problem:

    To attach a sample workbook. Make sure there is just enough data to make it clear what is needed. Include BEFORE/AFTER sheets if needed to show the process you're trying to complete or automate. Remember to desensitize the data.

    Click on GO ADVANCED and click "manage attachments" to open the upload window.


    To add a file to a post

  3. #3
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Can we prevent Save option availability if it does not meet certain criteria?

    You could try a "Private Sub Workbook_BeforeSave" macro.

    Right click on a tab in the workbook were you wish to use the macro, then click on "View Code". In the new windows that open find small window with heading "Project - VBAProject" and right click on "ThisWorkbook" and click on "View code". In the new windows that open paste the the code I've written.

    Macro will loop through all sheets in the workbook and when it find a cell colored red will inform that there is a problem with the data and activate the sheet where the red cell is found before macro excits without saving file.

    Be aware that macro will react on the first found red cell and exit with a warning without saving the file. If there are more errors then a modification could be made to report all errors on the different sheets i.e. cell address and sheet name.

    Please Login or Register  to view this content.
    Alf
    Last edited by Alf; 12-11-2018 at 06:51 AM.

  4. #4
    Registered User
    Join Date
    12-10-2018
    Location
    Chennai
    MS-Off Ver
    Excel 2016
    Posts
    3

    Re: Can we prevent Save option availability if it does not meet certain criteria?

    I have seen your code and tried to work it out. But did not end good.

    I have attached my excel file here which contains one red coloured cell which is an error.

    Can you please check if that code works on my excels and recommend me procedure ?
    Attached Files Attached Files

  5. #5
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Can we prevent Save option availability if it does not meet certain criteria?

    Well, there are red and red. when I used "vbRed" this is the strongest red color i.e. = 3 so whatever shade of red you are using you must find the colour code for that

    I've modified your file so you can save it, them open it and run the normal macro "MakeRed". This will set the range A102:M102 on sheet "Compare" to the "vbRed" color.

    After doing so you can now try to save this file and see what happens when color is set to match the color specified in macro.

    To find what code gives what color you could run this macro in a workbook:

    Please Login or Register  to view this content.
    Alf
    Attached Files Attached Files

  6. #6
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Can we prevent Save option availability if it does not meet certain criteria?

    doing a quick check on the color you used in sheet "Compare" range A102:M102 you need to change the line

    Please Login or Register  to view this content.
    to this

    Please Login or Register  to view this content.
    in order to get the macro to recognize the color you used.

    Alf

  7. #7
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,481

    Re: Can we prevent Save option availability if it does not meet certain criteria?

    @Alf
    If you checked for the Interior.color you will find the color to find.
    Please Login or Register  to view this content.
    So using your loop
    Please Login or Register  to view this content.

  8. #8
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Can we prevent Save option availability if it does not meet certain criteria?

    Had another go at it on my second PC and now the line

    Please Login or Register  to view this content.
    don't work so after testing a bit more on my second PC I found this line work

    Please Login or Register  to view this content.
    It looks like how mixed colors are defined may depend on the PC??? Sounds strange so my best advice to you is to record a macro when you select the color and then take the color definition from that marco unless you use a clearly defined color like for instance "vbRed"

    Alf

  9. #9
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,481

    Re: Can we prevent Save option availability if it does not meet certain criteria?

    ............................

    It looks like how mixed colors are defined may depend on the PC??? Sounds strange so my best advice to you is to record a macro when you select the color and then take the color definition from that marco unless you use a clearly defined color like for instance "vbRed"

    Alf
    I suggested how to get the interior.color
    Last edited by davesexcel; 12-12-2018 at 02:45 PM.

  10. #10
    Registered User
    Join Date
    12-10-2018
    Location
    Chennai
    MS-Off Ver
    Excel 2016
    Posts
    3

    Smile Re: Can we prevent Save option availability if it does not meet certain criteria?

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Dim wks As Worksheet
    Dim cell As Range

    For Each wks In ActiveWorkbook.Worksheets

    For Each cell In wks.UsedRange
    If cell.Interior.Color = RGB(250,128,114) Then
    MsgBox ("Error in Data, workbook will no be saved!")
    wks.Activate
    Cancel = True
    Exit Sub
    End If
    Next

    Next

    End Sub





    Thank you so much for your kind words , the above code worked fine.
    I changed the color code in the code.

    Can you extend your help once more please ?

    Now, I have attached the same excel below, in that there is a sheet named ddd .

    In that, cells A1A2, B1B2 will always be in RGB(250,128,114).

    I want to exclude A1B1 always .(with text or without text)

    I want to include A2B2 if there is text then say "indefinite error" . OR ELSE exclude.

    The same with A3B3, A4B4, A5B5 and so on,if there is text then
    say "indefinite error" . OR ELSE exclude.(incase text is there, the cell will be in RGB(250,128,114)).


    How can we put this in the code? Is it possible?
    Attached Files Attached Files

  11. #11
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Can we prevent Save option availability if it does not meet certain criteria?

    Hi David

    I suggested how to get the interior.color
    You certainly did but when I posted I only saw mine postings. This is probably caused by the fact that all postings are adjusted to GMT when they are posted so when persons are in different time zones .....

    This is not the first time something like this has happened i.e. when I post I'm the only one that have posted in the thread but when I check the next morning somebody has made an earlier posting than mine.

    Alf

  12. #12
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Can we prevent Save option availability if it does not meet certain criteria?

    Hi akipaul

    I'll have a look at new request and see if I come up with something useful.

    Alf

  13. #13
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Can we prevent Save option availability if it does not meet certain criteria?

    At the moment I'm not sure I understand you new request.

    You asked for and got a macro that stops the saving of a file if a cell or cells on sheet or on different sheets. From your uploaded file the stop trigger RGB code is (250, 128, 114)

    Now you ask that on sheet ddd that if A2B2 contains text "Infinite error" than these cells should be colored using RGB(250, 128, 114) and all other cells that contain this text.

    Should both A2 and B2 contain this text or if only one cell A2 or B2 contain "Infinite error" still both cells shall be colored RGB(250, 128, 114)

    The fastest way to color cells in A2:BXX" range would to set an autofilter to find all the cells on sheet ddd that match the "Infinite error" criteria, color these using RGB(250, 128, 114) and the remove the autofilter.

    But do you still test if any cell has the RGB(250, 128, 114) color because this means you can't save the file. Also you do say that A1A2 and B1B2 always have the RGB(250, 128, 114) color setting so I'm really puzzled.

    Alf

+ 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] IF function - To get value if criteria meet on col H (fall back on col F if not meet)
    By Bubbles_A in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 07-13-2018, 11:52 AM
  2. enable option button to be click together if condition meet
    By jp16 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-01-2018, 12:24 PM
  3. How to prevent user to save doc if the rows does not meet the criteria
    By filipal in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-24-2017, 02:22 PM
  4. prevent save if cell criteria not met
    By bqheng in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-01-2015, 08:06 PM
  5. How to delete columns that meet one criteria AND does NOT meet the other?
    By lch93lily in forum Excel - New Users/Basics
    Replies: 3
    Last Post: 08-06-2014, 09:53 AM
  6. [SOLVED] macro that will copy from (Leave availability worksheet) which meets criteria
    By Luphai in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 02-12-2013, 05:29 AM
  7. Prevent print macro unless condition meet
    By jimmy@9830 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-06-2007, 05:22 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