+ Reply to Thread
Results 1 to 7 of 7

Prohibit Saving an Excel Files if Conditions not Completed

Hybrid View

  1. #1
    Registered User
    Join Date
    03-18-2013
    Location
    medellin, colombia
    MS-Off Ver
    Excel 2003
    Posts
    3

    Prohibit Saving an Excel Files if Conditions not Completed

    hello,
    I have an Excel 2003 file where I would like to forbid people to save it unless some particular cell have "0" (zero) as a value.

    with a msgBox that would say that those cells need to be at zero in order for them to be able to save the file.

    Looking around i found the Workbook_BeforeSave but do not really know how to use it : i have come so far to a code that look like this :

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    
    If Worksheets("Sheet1").Cells(D1,e1,f1,g1,h1,i1).Value <> 0 Then
    
            Cancel = True
    MsgBox "Your Modification Impacted the Utiliti, Please Register Your Impact in Order to be Able to Save your Contribution to the File!", vbOKOnly
          Else
            Cancel = False
       End If
    End Sub
    thanks everyone for your help

  2. #2
    Forum Contributor
    Join Date
    12-31-2012
    Location
    Jhang, Pakistan
    MS-Off Ver
    Excel 2010
    Posts
    250

    Re: Prohibit Saving an Excel Files if Conditions not Completed

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    For i = 4 To 9
        If Cells(1, i) <> 0 Then
            Cancel = True
            MsgBox "Your Modification Impacted the Utiliti, Please Register" & _
            " Your Impact in Order to be Able to Save your Contribution to the File!", vbOKOnly
            Exit For
        End If
    Next
    End Sub

  3. #3
    Registered User
    Join Date
    03-18-2013
    Location
    medellin, colombia
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: Prohibit Saving an Excel Files if Conditions not Completed

    Thanks, i just added
        If Worksheets("Hoja1").Cells(1, i) <> 0 Then
    in order to have it with the first sheet.

    Thanks a lot

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,042

    Re: Prohibit Saving an Excel Files if Conditions not Completed

    Maybe, something like:

    Option Explicit
    
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Dim awf As WorksheetFunction: Set awf = WorksheetFunction
    
    If awf.Sum(Worksheets("Sheet1").Range("D1:I1")) <> 0 Then
        Cancel = True
        MsgBox "Your Modification Impacted the Utiliti." & _
               vbCrLf & vbCrLf & _
               "Please Register Your Impact in Order to be Able to Save your Contribution to the File!", _
               vbOKOnly
    Else
        Cancel = False
    End If
    
    End Sub

    Beware that a combination of positive and negative values in the range could result in a zero total ... and allow the workbook to be saved.


    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,042

    Re: Prohibit Saving an Excel Files if Conditions not Completed

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save


    You may also want to consider thanking those people who helped you by clicking on the little star at the bottom left of their reply to your question.

  6. #6
    Registered User
    Join Date
    03-18-2013
    Location
    medellin, colombia
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: Prohibit Saving an Excel Files if Conditions not Completed

    The Good Solution is the one from hafizimran

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    For i = 4 To 9
        If Worksheets("Sheet1").Cells(1, i) <> 0 Then
            Cancel = True
            MsgBox "You Generated an Impact in The Op Utility!" & _
            "Register your Contribution in Order to Be Able to Save", vbOKOnly
            Exit For
        End If
    Next
    End Sub

    Thanks to you too TMShucks but your Solution was too complicated and as you underligned it yourself, it was a Sum of the Range so...
    But thank you very much for your quick answer.

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,042

    Re: Prohibit Saving an Excel Files if Conditions not Completed

    You're welcome. It's always good to have choices and you learn from the different options presented.

    Glad you have a solution you like.

    Regards, TMS

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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