Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in
x
or
x
x
Register
x

or

How to prevent saving if specific cell is blank in Excel?

For example, you designed a form in worksheet and share with your colleagues. You hope your colleagues fill their names into the specific cell to indicate who entered this form, otherwise prevent them from saving the form, how could you do? Here I will introduce a VBA macro to prevent saving a workbook if the specific cell is blank in Excel.

Office Tab Enable Tabbed Editing and Browsing in Office, and Make Your Work Much Easier...
Kutools for Excel Solves Most of Your Problems, and Increases Your Productivity by 80%
  • Reuse Anything: Add the most used or complex formulas, charts and anything else to your favorites, and quickly reuse them in the future.
  • More than 20 text features: Extract Number from Text String; Extract or Remove Part of Texts; Convert Numbers and Currencies to English Words.
  • Merge Tools: Multiple Workbooks and Sheets into One; Merge Multiple Cells/Rows/Columns Without Losing Data; Merge Duplicate Rows and Sum.
  • Split Tools: Split Data into Multiple Sheets Based on Value; One Workbook to Multiple Excel, PDF or CSV Files; One Column to Multiple Columns.
  • Paste Skipping Hidden/Filtered Rows; Count And Sum by Background Color; Send Personalized Emails to Multiple Recipients in Bulk.
  • Super Filter: Create advanced filter schemes and apply to any sheets; Sort by week, day, frequency and more; Filter by bold, formulas, comment...
  • More than 300 powerful features; Works with Office 2007-2019 and 365; Supports all languages; Easy deploying in your enterprise or organization.


arrow blue right bubblePrevent saving if specific cell is blank in Excel

To prevent from saving current workbook if the specific cell is blank in Excel, you can apply the following VBA macro easily.

Step 1: Open the Microsoft Visual Basic for Applications window with pressing the Alt + F11 keys in a meantime.

Step 2: In the Project Explorer, expand the VBAProject (Your workbook name.xlsm) and Microsoft Excel Objects, and then double click the ThisWorkbook. See screen shot left:

Step 3: In the opening ThisWorkbook window, paste the following VBA macro:

VBA Macro: Prevent saving if specific cell is blank

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Application.Sheets("TEST").Range("A1").Value = "" Then
Cancel = True
MsgBox "Save cancelled"
End If
End Sub
Note: In the VBA code, the "TEST" is the specific worksheet name, and the "A1" is the specific cell, and you can change them as you need.

Now if the specific cell is blank in the current workbook, when you save it, a warning dialog box comes out and tells you "Save cancelled". See the following screen shot:


arrow blue right bubbleRelated Articles


Kutools for Excel Solves Most of Your Problems, and Increases Your Productivity by 80%

  • Reuse: Quickly insert complex formulas, charts and anything that you have used before; Encrypt Cells with password; Create Mailing List and send emails...
  • Super Formula Bar (easily edit multiple lines of text and formula); Reading Layout (easily read and edit large numbers of cells); Paste to Filtered Range...
  • Merge Cells/Rows/Columns without losing Data; Split Cells Content; Combine Duplicate Rows/Columns... Prevent Duplicate Cells; Compare Ranges...
  • Select Duplicate or Unique Rows; Select Blank Rows (all cells are empty); Super Find and Fuzzy Find in Many Workbooks; Random Select...
  • Exact Copy Multiple Cells without changing formula reference; Auto Create References to Multiple Sheets; Insert Bullets, Check Boxes and more...
  • Extract Text, Add Text, Remove by Position, Remove Space; Create and Print Paging Subtotals; Convert Between Cells Content and Comments...
  • Super Filter (save and apply filter schemes to other sheets); Advanced Sort by month/week/day, frequency and more; Special Filter by bold, italic...
  • Combine Workbooks and WorkSheets; Merge Tables based on key columns; Split Data into Multiple Sheets; Batch Convert xls, xlsx and PDF...
  • More than 300 powerful features. Supports Office/Excel 2007-2019 and 365. Supports all languages. Easy deploying in your enterprise or organization. Full features 30-day free trial.
kte tab 201905

Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier

  • Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
  • Open and create multiple documents in new tabs of the same window, rather than in new windows.
  • Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
officetab bottom
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Benjamin · 3 months ago
    good afternoon, I used the code above and it worked perfectly. my question is what should the code look like if I want to test on 2 cells? I am quite desperate. thanking you I advance for your assistance
  • To post as a guest, your comment is unpublished.
    Yzelle · 4 months ago
    I have a very big spreadsheet that contains a lot of info.
    Can someone please help me with a code to copy into VBA - I want it to be that if Cell C2-C1000+ have any info in them then cell O2-O1000+ and P2-P1000+ requires user input - however if a cell in Column C is empty then the cell in Column O & P can be empty as well. (for example) if cell C3 doesn't have any data input then cell O3-P3 can be empty.

    Thank you :)
    • To post as a guest, your comment is unpublished.
      kellytte · 3 months ago
      Hi Yzelle,
      Please remember to place below code into “ThisWorkbook” script window, and rename the worksheet name “Test” in the below code based on your condition.

      Dim xIRg As Range
      Dim xSRg As Range
      Dim xBol As Boolean
      Dim xInt As Integer
      Dim xStr As String
      If ActiveSheet.Name = "Test" Then
      Set xRg = Range("C:C")
      Set xRRg = Intersect(xRg.Worksheet.UsedRange, xRg)
      xBol = False
      On Error Resume Next
      For xInt = 1 To xRRg.Count
      Set xIRg = xRRg.Item(xInt)
      If xIRg.Value2 <> "" Then
      Set xSRg = Nothing
      If (Range("O" & xIRg.Row) = "") Or (Range("P" & xIRg.Row) = "") Then
      xBol = True
      Exit For
      End If
      End If
      Next
      If xBol Then
      Cancel = True
      MsgBox "Save cancelled"
      End If
      End If
      End Sub
  • To post as a guest, your comment is unpublished.
    Andrew Gonzales · 5 months ago
    This is really great. Do you know what I can do to make this work for a range of sheets and a number of cells? Also, these cells cannot always be the same, as there are sheets generated in this specific workbook which may not have the same cell needing to be filled each time. The cells will always be in the same column, just above the page border which is also generated. Thanks!
  • To post as a guest, your comment is unpublished.
    M H · 5 months ago
    Hi, very useful. BUT there is a problem when I use it for files on the sharepoint. The changes are not saved but a new version is created that is displayed when reopening which is quite confusing. Is it possible to disable these new versions ?
  • To post as a guest, your comment is unpublished.
    Wkai · 8 months ago
    Hi i want to ask if it is from A2 to U2. what should i write?
    • To post as a guest, your comment is unpublished.
      kellytte · 4 months ago
      Hi Wkai,
      Try this VBA code:
      (This VBA code will detect Range A2:E5 in the Sheet “Test”, and cancel saving if there are blank cells existing in the range.)

      Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
      Dim xWSName As String
      Dim xRgAddress As String
      Dim xRg As Range
      Dim xWs As Worksheet
      Dim xFNRg As Range
      xWSName = "TEST"
      xRgAddress = "A2:E5"
      Set xWs = Application.ActiveWorkbook.Worksheets.Item(xWSName)
      Set xRg = xWs.Range(xRgAddress)
      Set xFNRg = Nothing
      On Error Resume Next
      Set xFNRg = xRg.SpecialCells(xlCellTypeBlanks, 23)
      If Not TypeName(xFNRg.count) = "Nothing" Then
      Cancel = True
      MsgBox "Save cancelled"
      End If
      End Sub
  • To post as a guest, your comment is unpublished.
    Eduardo · 2 years ago
    hi - this was super helpful... Just had one question, how do I save the file without a value in that field? As I try to save, the VBA code will pop the "Save Cancelled" message which is the intended response, however, need to save once without a value to create the form to be reused.

    Thanks!
    • To post as a guest, your comment is unpublished.
      Tang Kelly · 2 years ago
      Hi Eduardo,
      What about typing a space in the specified cell to pretend to a blank cell? Please remind to remove the space in future!