BeforeSave Event

ctmerced

New Member
Joined
Jun 28, 2019
Messages
3
Hello-
New to VB scripting. Here's what I'm trying do.
1) Prevent the user from saving the spreadsheet, unless the value within a column range is either "YES" or "NO". I'm providing the user with a default value on each of the cells that they must change. If the user tries to save the spreadsheet without changing the value to either "YES" or "NO", then I would like to display a message and return them to the spreadsheet.

Any help will be greatly appreciated.

Carlos
 

Some videos you may like

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
9,198
Place this macro 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 macro into the empty window that opens up. Change the sheet name and range (in red) to suit your needs. Close the window to return to your sheet.
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Application.ScreenUpdating = False
    Dim LastRow As Long, rng As Range
    LastRow = Sheets("[COLOR="#FF0000"]Sheet1[/COLOR]").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    For Each rng In Sheets("[COLOR="#FF0000"]Sheet1").Range("A2:A"[/COLOR] & LastRow)
        If rng <> "YES" And rng <> "NO" Then
            MsgBox ("Cell " & rng.Address(0, 0) & " must be 'YES' or 'NO'.")
            Cancel = True
        End If
    Next rng
    Application.ScreenUpdating = True
End Sub
 
Last edited:

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
16,952
Office Version
2010
Platform
Windows
You can greatly improve your chances of getting a solution if you provide some detail about your workbook.
On what sheet (sheet name) or sheets does the column range you want to monitor exist? What is the exact address of the range?
 

Watch MrExcel Video

Forum statistics

Threads
1,102,782
Messages
5,488,850
Members
407,658
Latest member
Arias610

This Week's Hot Topics

  • Timer in VBA - Stop, Start, Pause and Reset
    [CODE=vba][/CODE] Option Explicit Dim CmdStop As Boolean Dim Paused As Boolean Dim Start Dim TimerValue As Date Dim pausedTime As Date Sub...
  • how to updates multiple rows in muliselect listbox
    Hello everyone. I need help with below code. code is only chaning 1st row in mulitiselect list box. i know issue with code...
  • Delete Row from Table
    I am trying to delete a row from a table using VBA using a named range to find what I need to delete. My Range is finding the right cell. In the...
  • Assigning to a variable
    I have a for each block where I want to assign the value in column 5 of the found row to the variable Serv. [CODE=vba] For Each ws In...
  • Way to verify information
    Hi All, I don't know what to call this formula, and therefore can't search. I have a spreadsheet with information I want to reference...
  • Active Cell Address – Inactive Sheet
    How to use VBA to get the cell address of the active cell in an inactive worksheet and then place that cell address in a location on the current...
Top