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
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
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:
Upvote 0
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?
 
Upvote 0

Forum statistics

Threads
1,223,705
Messages
6,173,985
Members
452,540
Latest member
haasro02

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