+ Reply to Thread
Results 1 to 3 of 3

Code, that disables save, save as and close commands

Hybrid View

  1. #1
    Registered User
    Join Date
    10-30-2009
    Location
    Europe
    MS-Off Ver
    Excel 2003
    Posts
    9

    Code, that disables save, save as and close commands

    Each of the worksheets in my model use A1 as a control cell for any errors and inconsistencies. My aim is to disable save and close commands in case A1 is not equal to 0 in any of the worksheets.

    The code I currently use for that purpose is as follows.

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    
    Dim Rng As Range
    Dim ws As Worksheet
    
    For Each ws In Worksheets
    Set Rng = ws.Range("A1")
    
    If ws.Range > 0 Then
    Cancel = True
    lReply = MsgBox("Please correct...", vbOKOnly)
    
    End If
    End Sub
    Something however is not OK and it gives errors. Furthermore, the disabling of close command is not covered.

    Please help me update it.
    Last edited by mgmetev; 11-02-2009 at 06:32 PM.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Code, that disables save, save as and close commands

    Maybe like this:
    Option Explicit
    
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
        Cancel = Not CheckA1
    End Sub
    
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
        Cancel = Not CheckA1
    End Sub
    
    Private Function CheckA1() As Boolean
        Dim wks         As Worksheet
    
        For Each wks In Me.Worksheets
            If wks.Range("A1").Value <> 0 Then
                Application.Goto wks.Range("A1")
                MsgBox Prompt:="Save or Close cancelled." & vbLf & vbLf & _
                            "This should be zero. Please correct.", _
                       Buttons:=vbOKOnly, _
                       Title:="Not good!"
                Exit Function
            End If
        Next wks
        CheckA1 = True
    End Function
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    10-30-2009
    Location
    Europe
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Code, that disables save, save as and close commands

    Hip hip, that's exactly what I wanted
    Thanks shg, great job! Another beer for you.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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