VBA Saving an excel spreadsheet only on fulfilling a particular condition.

I am in need of vba code for excel 2007 wherein the excel sheet whenever is save then before saving it should check that in “E1″ i.e. the range “E1″ <> 0 then only the file should save that is the excel sheet should be save only if the E1 contain “0”. Otherewise a dialog box should appear with a msg that the “TRIAL BALANCE NOT TALLY” hence the file cannot be saved.


Contributor Asked on July 12, 2015 in VBA: Macros.
Add Comment
1 Answer(s)
Best answer

Hi Ashish,

Here is the VBA Macro to allow the file to Save if Range(“E1″) is 0, other wise it will not save and trigger the message. 

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Sheets("YourE1RangeSheet").Range("E1").Value <> 0 Then
MsgBox "TRIAL BALANCE NOT TALLY", vbInformation, "Please Check"
Cancel = True
End If
End Sub

How to use this macro?

  1. Open VBA Editor
  2. Open on ‘ThisWorkbook’ Module (double click on the ThisWorkbook class in the project explorer
  3. Copy the above macro and Paste it in the ‘ThisWorkbook’ Code Module
  4. Chnage ‘YourE1RangeSheet’ to your sheet name of Range E1


Keymaster Answered on July 12, 2015.
Add Comment
  • Found this useful?

    Please share using the share button above.

    If you found the answer is best answer for your question, Please mark as 'best answer' by clicking on the right tick mark icon at the left side of the answer.

    Found the answer useful and wants to credit the user, then vote the answer (vote up).

  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.