Prevent saving a worksheet is some cells contain certain text!

josullivan601632

New Member
Joined
Aug 23, 2020
Messages
39
Office Version
  1. 2007
Platform
  1. Windows
Hi everyone. I have created a rather nifty worksheet for my colleague to complete, I have added a macro that jumps to the next cell to complete so users dont have to manually move to the next cell and that works great. There are cells which at the moment I have a drop down box for users to select "yes" or "no" and the master is saved with "select" in those cells to prompt the user to fill in that cell. As the "yes" and "no" options are really important and the users are now forgetting to "select", I want to add something that stops the users saving the worksheet if those cells still contain "select". Can I do this, would this effect the other macro I have? Any help would be fantastic!
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
In which sheet and cells is the drop down list with "Yes" and "No"?
 
Last edited:
Upvote 0
This does not answer your question, but it may help by providing a stronger hint. It simply expands your validation list when the cell is selected. The user cannot tab out of it, but must select an item or press the escape key. There may be other ways, but at his point, the use's actions would be deliberate and not forgetful. I would use a named range that contains your Yes/No validation cells and forego using the 'HasYesNoValidation' function.

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.CountLarge = 1 Then
        If HasYesNoValidation(Target) Then
            With Target.Validation
                .Delete
                .Add xlValidateList, xlValidAlertStop, , "Yes, No"
                .InCellDropdown = True
                .ErrorMessage = "Please select 'Yes' or 'No'"
                .ShowError = True
                .IgnoreBlank = False
            End With
            Application.SendKeys "%{DOWN}"
        End If
    End If
End Sub

Private Function HasYesNoValidation(Target As Range)
    On Error Resume Next
    HasYesNoValidation = InStr(Target.Validation.Formula1, "Yes, No")
    On Error GoTo 0
End Function
 
Upvote 0
This does not answer your question, but it may help by providing a stronger hint. It simply expands your validation list when the cell is selected. The user cannot tab out of it, but must select an item or press the escape key. There may be other ways, but at his point, the use's actions would be deliberate and not forgetful. I would use a named range that contains your Yes/No validation cells and forego using the 'HasYesNoValidation' function.

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.CountLarge = 1 Then
        If HasYesNoValidation(Target) Then
            With Target.Validation
                .Delete
                .Add xlValidateList, xlValidAlertStop, , "Yes, No"
                .InCellDropdown = True
                .ErrorMessage = "Please select 'Yes' or 'No'"
                .ShowError = True
                .IgnoreBlank = False
            End With
            Application.SendKeys "%{DOWN}"
        End If
    End If
End Sub

Private Function HasYesNoValidation(Target As Range)
    On Error Resume Next
    HasYesNoValidation = InStr(Target.Validation.Formula1, "Yes, No")
    On Error GoTo 0
End Function

That certainly would give the same result! I am not confident with macros, I have one on the worksheet already for the user to just tab into the cells for completing which is nice, do I just copy and paste the above at the end of that macro, something tells me it wouldn't be that straight forward!
 
Upvote 0
Place these macros in the code module for ThisWorkbook. Do the following: Hold down the ALT key and press the F11 key. This will open the Visual Basic Editor. In the left hand pane, double click on "ThisWorkbook". Copy/paste the macros into the empty window that opens up. Close the window to return to your sheet. These two macros will check the range M46:M52 for the word "Select". If any cell in that range contains "Select", the user will be prompted to select "Yes" or "No" in that cell and will not be allowed to save or close the file. You can use both macros or only the Workbook_BeforeSave macro if you don't want to check the range if the user tries to close the file.
VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Application.ScreenUpdating = False
    Dim rng As Range
    For Each rng In Sheets("BuyersWorksheet").Range("M46:M52")
        If rng = "Select" Then
            MsgBox ("Please select 'Yes' or 'No' in cell " & rng.Address(0, 0))
            Cancel = True
            Exit For
        End If
    Next rng
    Application.ScreenUpdating = True
End Sub

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Application.ScreenUpdating = False
    Dim rng As Range
    For Each rng In Sheets("BuyersWorksheet").Range("M46:M52")
        If rng = "Select" Then
            MsgBox ("Please select 'Yes' or 'No' in cell " & rng.Address(0, 0))
            Cancel = True
            Exit For
        End If
    Next rng
    Application.ScreenUpdating = True
End Sub
These macros should affect any other macros you are currently using.
 
Upvote 0
Hi,
you could create simple function to Cancel the Close or Save events

Thisworkbook code page
VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Cancel = NotComplete
End Sub

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Cancel = NotComplete
End Sub

Standard module
VBA Code:
Function NotComplete() As Boolean
    Dim Cell As Range, Rng As Range
    
    Set Rng = Worksheets("BuyersWorksheet").Range("M46:M52")
    For Each Cell In Rng.Cells
        NotComplete = IsError(Application.Match(Cell.Value, Array("Yes", "No"), 0))
        If NotComplete Then
             Cell.Parent.Activate
             Cell.Select
            MsgBox "Please Select Yes or No From List", 48, "Entry Required"
            Exit For
        End If
    Next Cell
End Function

Dave
 
Upvote 0
I tried the codes above but I can still save the worksheet with those cells having "Select" in the cell from the droplist! Any ideas what I am doing wrong?
 
Upvote 0
Just tested my suggestion & worked OK with "Select" in range

Have you placed codes where directed?

Dave
 
Upvote 0
The macros in Post #6 also worked when tested.
 
Upvote 0

Forum statistics

Threads
1,215,280
Messages
6,124,034
Members
449,139
Latest member
sramesh1024

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top