Save excel file based on a cell condition

russelldt

Board Regular
Joined
Feb 27, 2021
Messages
160
Office Version
  1. 365
Platform
  1. MacOS
I want to be able to save an excel file, only when specific data have been entered

I can create an if(and( formula to validate the input that is required, and place the result “1” in cell A1. If cell A1 is 1, the file can be saved, (not save as). If cell A1 is ‘0’, a message should appear to complete data in the cells.

Thanks
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
VBA Code:
Sub Macro1()

If Sheets("Sheet1").Range("A1") = 1 Then
    ActiveWorkbook.Save
Else
    MsgBox "You are missing data"
End If

End Sub
 
Upvote 0
VBA Code:
Sub Macro1()

If Sheets("Sheet1").Range("A1") = 1 Then
    ActiveWorkbook.Save
Else
    MsgBox "You are missing data"
End If

End Sub
Thanks for the prompt reply


This works (ie I get the error message when the condition is not met) when I test run it in the VBA developer mode, but as soon as I return to the actual sheet (i replaced Sheet1 with Cashflow input) I am able to save it without the condition being met.
 
Upvote 0
Thanks for the prompt reply


This works (ie I get the error message when the condition is not met) when I test run it in the VBA developer mode, but as soon as I return to the actual sheet (i replaced Sheet1 with Cashflow input) I am able to save it without the condition being met.
I have assumed that I would be able to Save the file as normal, i.e not using a macro button.
 
Upvote 0
It’s numerical, but I can change that to a True, depending on how I set up my If(and( criteria. To test it now, I just put a 1 on cell A1
 
Upvote 0
Save this not in a seperate module, but in the ThisWorkbook section of VBA.

VBA Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

If Sheets("Sheet1").Range("A1") = 1 Then
    ActiveWorkbook.Save
Else
    MsgBox "You are missing data"
    Cancel = True
End If

End Sub
 
Upvote 0
Save this not in a seperate module, but in the ThisWorkbook section of VBA.

VBA Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

If Sheets("Sheet1").Range("A1") = 1 Then
    ActiveWorkbook.Save
Else
    MsgBox "You are missing data"
    Cancel = True
End If

End Sub
I added the VBA to the ThisWorkbook and when i run it, the Macro Name window pops up
 
Upvote 0

Forum statistics

Threads
1,223,882
Messages
6,175,165
Members
452,615
Latest member
bogeys2birdies

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