Before Close, Before Save not working

Posted on 2011-09-07
Last Modified: 2012-05-12
Hi Experts,

I asked this question in a previously, and I thought this was working but it doesn't appear to be.

The Before Save and Before Close do not appear to be working. What I'm attempting is not to be able to close or save the workbook as long as their are data on worksheet TEST that are red.  I have attached a copy of the file for review.

The way its working now is that it will give you the error on the worksheet TEST, but if you go to TEST Wire, it will allow you to close the workbook.
Sub DoCloseStuff(Cancel As Boolean)
Call CheckData(Cancel)
    If Cancel = False Then Call ClearContents(Cancel)
Call ClearContents(Cancel)
End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Call DoCloseStuff(Cancel)
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call DoCloseStuff(Cancel)
End Sub
Sub ClearContents(Cancel As Boolean)
Dim i As Long
Dim bTest As Boolean
Dim wks As Worksheet

For Each wks In ActiveWorkbook.Worksheets
    bTest = False
    If InStr(LCase(wks.Name), "wire") > 0 Then
        On Error Resume Next
        'On Error GoTo 0
        For i = 6 To 44
            If InStr(LCase(wks.Name), "wire") > 0 Then
                If Range("B" & i).Value = " " Then
                    wks.Range("i" & i & ":k" & i).Cells.SpecialCells(xlTextValues).ClearContents
                End If
            End If
            If False Then bTest = True      'You need to have a valid test here
        Next i
        On Error Resume Next
        On Error GoTo 0
        '***I don't know what you are checking, but you need a way to bypass this statement if everything is OK
        'If bTest Then
           ' MsgBox "Cannot SAVE or CLOSE file! All Cells Colored Red need to be filled in", vbExclamation, "Close Cancelled"
            'Cancel = True
        End If
Next wks
End Sub

Sub CheckData(Cancel As Boolean)
Dim i As Long
Dim blFailed As Boolean
Dim col As Long
col = 19
col2 = 3


For i = 12 To 50
    With ActiveSheet.Cells(i, "F")
        If .Value = "MAX" Then
           If .Offset(0, -1).Value = "" Then
                 blFailed = True
                 .Offset(0, -1).Interior.ColorIndex = col2
                 .Offset(0, -1).Interior.ColorIndex = col
             End If
             If .Offset(0, -2).Value = "" Then
                  blFailed = True
                 .Offset(0, -2).Interior.ColorIndex = col2
                 .Offset(0, -2).Interior.ColorIndex = col
             End If
             If .Offset(0, -4).Value = "" Then
                 blFailed = True
                 .Offset(0, -4).Interior.ColorIndex = col2
                 .Offset(0, -4).Interior.ColorIndex = col
             End If
             If .Offset(0, 3).Value = "" Then
                 blFailed = True
                 .Offset(0, 3).Interior.ColorIndex = col2
                 .Offset(0, 3).Interior.ColorIndex = col
             End If
             If .Offset(0, 19).Value = "" Then
                 blFailed = True
                 .Offset(0, 19).Interior.ColorIndex = col2
                 .Offset(0, 19).Interior.ColorIndex = col
             End If
        End If
    End With
Next i

    If blFailed Then
        MsgBox "Cannot SAVE or CLOSE file! All Cells Colored Red need to be filled in", vbExclamation, "Save Cancelled"
        Cancel = True
    End If

End Sub

Open in new window

Question by:mato01
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 33

Expert Comment

by:Rob Henson
ID: 36501168
The Workbook Subs need to be in the This Workbook module in the VBE. Is this the case?

Rob H

Author Comment

ID: 36503030
They are in This Workbook.  See Attached File.
LVL 33

Accepted Solution

Rob Henson earned 250 total points
ID: 36511889
I would ahve thought the other subs need to be in a separate module with only the beforeclose and beforesave in the "this workbook" sheet/module.

In the vbe use the insert menu to insert a module and then cut and paste the other subs to this module.

Rob H

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question