VBA To prevent document save unless criteria is met

MrRosco

New Member
Joined
May 12, 2016
Messages
47
Hello wonderful Mr Excel forum.

Could i have some assistance on creating VBA code to prevent a document save unless certain criteria is met.

i have four codes within a drop down which relate to invoices, however one is a general code and require information is their a way to stop the document saving unless information
is added into the comments section.

The code in question is 609110 which is located in cell H and the comments box is located in O

in addition would it also be possible to add a message box to say "Please add comment"

Thanks
Rosco
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
You could put this in the ThisWorkbook code module. (Adjust the sheet reference to suit)

Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    
    Cancel = 0 < WorksheetFunction.CountIfs(Sheet1.Range("H:H"), 609110, Sheet1.Range("O:O"), vbNullString)
    
    If Cancel Then
        MsgBox "add a comment"
    End If
    
End Sub
 
Upvote 0
A variation
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Dim badRow As Long
    badRow = Evaluate("MAX((Sheet1!H:H=609110)*(Sheet1!O:O="""")*ROW(H:H))")
    
    Cancel = 0 < badRow
    
    If Cancel Then
        Application.Goto Sheet1.Cells(badRow, "H"), Scroll:=True
        ActiveCell.EntireRow.Range("O1").Select
        MsgBox "add a comment in O" & badRow
    End If
    
End Sub
 
Upvote 0
Thanks Mike,

I have tried using this code with no luck, I think it might need some kind of look to check all the rows within the document, as their will be more than one entry. again I don't know if this will be possible to do or not
thanks

Rosco
 
Upvote 0

Forum statistics

Threads
1,214,551
Messages
6,120,159
Members
448,948
Latest member
spamiki

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