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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

622 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