Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Before Close, Before Save not working

Posted on 2011-09-07
3
Medium Priority
?
780 Views
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
        wks.Activate
        
        On Error Resume Next
        wks.Unprotect
        '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
        
        wks.Range("A6").Select
        On Error Resume Next
        wks.Protect
        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

ActiveSheet.Activate
ActiveSheet.Unprotect

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
             Else
                 .Offset(0, -1).Interior.ColorIndex = col
             End If
             
             If .Offset(0, -2).Value = "" Then
                  blFailed = True
                 .Offset(0, -2).Interior.ColorIndex = col2
             Else
                 .Offset(0, -2).Interior.ColorIndex = col
             End If
             If .Offset(0, -4).Value = "" Then
                 blFailed = True
                 .Offset(0, -4).Interior.ColorIndex = col2
             Else
                 .Offset(0, -4).Interior.ColorIndex = col
             End If
             If .Offset(0, 3).Value = "" Then
                 blFailed = True
                 .Offset(0, 3).Interior.ColorIndex = col2
             Else
                 .Offset(0, 3).Interior.ColorIndex = col
             End If
             If .Offset(0, 19).Value = "" Then
                 blFailed = True
                 .Offset(0, 19).Interior.ColorIndex = col2
            Else
                 .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

ActiveSheet.Protect
Range("A12").Select
End Sub

Open in new window

0
Comment
Question by:mato01
  • 2
3 Comments
 
LVL 34

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?

Thanks
Rob H
0
 

Author Comment

by:mato01
ID: 36503030
They are in This Workbook.  See Attached File.
TestMacros-Check-1-.xls
0
 
LVL 34

Accepted Solution

by:
Rob Henson earned 1000 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.

Thanks
Rob H
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

782 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