Note: The other languages of the website are Google-translated. Back to English
登陆  \/ 
x
or
x
注册  \/ 
x

or

如果特定单元格在Excel中为空,如何防止保存?

例如,您在工作表中设计了一个表格并与同事共享。 您希望您的同事在特定单元格中填写其姓名,以指示谁输入了此表单,否则阻止他们保存该表单,您该怎么办? 在这里,我将介绍一个VBA宏,以防止在Excel中特定单元格为空白时保存工作簿。

Office选项卡在Office中启用选项卡式编辑和浏览,并使您的工作更加轻松...
Kutools for Excel解决了您的大多数问题,并使您的生产率提高了80%
  • 重用任何东西: 将最常用或最复杂的公式,图表等添加到您的收藏夹中,并在将来快速重用它们。
  • 超过20种文字功能: 从文本字符串中提取数字; 提取或删除部分文字; 将数字和货币转换为英文单词。
  • 合并工具:将多个工作簿和工作表合二为一; 合并多个单元格/行/列,而不会丢失数据; 合并重复的行和总和。
  • 分割工具:根据价值将数据分割成多个工作表; 一本工作簿可转换为多个Excel,PDF或CSV文件; 一列到多列。
  • 跳过粘贴 隐藏/过滤的行; 计数与求和 按背景色; 向多个收件人批量发送个性化电子邮件。
  • 超级过滤器: 创建高级过滤方案并应用于任何工作表; 排序 按星期,日期,频率等 滤波器 用粗体,公式,注释...
  • 超过300种强大的功能; 适用于Office 2007-2019和365; 支持所有语言; 在您的企业或组织中轻松部署。


箭头蓝色右气泡如果特定单元格在Excel中为空,则阻止保存

如果特定单元格在Excel中为空,则要防止保存当前工作簿,可以轻松应用以下VBA宏。

步骤1:按键,打开“ Microsoft Visual Basic for Applications”窗口 其他 + F11 同时键。

步骤2:在专案浏览器中,展开 VBAProject(您的工作簿名称.xlsm) 以及 Microsoft Excel对象,然后双击 的ThisWorkbook。 见左图:

步骤3:在打开的ThisWorkbook窗口中,粘贴以下VBA宏:

VBA宏:如果特定单元格为空,则防止保存

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
注意:在VBA代码中,“ TEST”是特定的工作表名称,而“A1是特定的单元格,您可以根据需要进行更改。

现在,如果当前工作簿中的特定单元格为空白,则在保存时,将出现一个警告对话框,告诉您“保存已取消”。请参见以下屏幕截图:


箭头蓝色右气泡相关文章


最佳办公效率工具

Kutools for Excel解决了您的大多数问题,并使您的生产率提高了80%

  • 重用: 快速插入 复杂的公式,图表 以及您以前使用过的任何东西; 加密单元 带密码 创建邮件列表 并发送电子邮件...
  • 超级公式栏 (轻松编辑多行文本和公式); 阅读版式 (轻松读取和编辑大量单元格); 粘贴到过滤范围...
  • 合并单元格/行/列 不会丢失数据; 拆分单元格内容; 合并重复的行/列...防止细胞重复; 比较范围...
  • 选择重复或唯一 行; 选择空白行 (所有单元格都是空的); 超级查找和模糊查找 在许多工作簿中; 随机选择...
  • 确切的副本 多个单元格,无需更改公式参考; 自动创建参考 到多张纸; 插入项目符号,复选框等...
  • 提取文字,添加文本,按位置删除, 删除空间; 创建和打印分页小计; 在单元格内容和注释之间转换...
  • 超级滤镜 (将过滤方案保存并应用于其他工作表); 高级排序 按月/周/日,频率及更多; 特殊过滤器 用粗体,斜体...
  • 结合工作簿和工作表; 根据关键列合并表; 将数据分割成多个工作表; 批量转换xls,xlsx和PDF...
  • 超过300种强大功能。 支持Office / Excel 2007-2019和365。支持所有语言。 在您的企业或组织中轻松部署。 完整功能30天免费试用。 60天退款保证。
kte选项卡201905

Office选项卡为Office带来了选项卡式界面,使您的工作更加轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,发布者,Access,Visio和Project。
  • 在同一窗口的新选项卡中而不是在新窗口中打开并创建多个文档。
  • 每天将您的工作效率提高50%,并减少数百次鼠标单击!
officetab底部
Say something here...
symbols left.
You are guest
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.
    Aditya · 8 months ago
    This is not working, it states save cancelled but still ends up saving the workbook
    • To post as a guest, your comment is unpublished.
      kellytte · 7 months ago
      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.

      For example, your sheet is named as "Sheet1", and the specified cell is B2, you need to change the sheet name and cell address in the VBA code before running it
  • To post as a guest, your comment is unpublished.
    Happy · 1 years ago
    I tried above formula which works. May i know is there any formula can force user to fill in before they can save? As i set the pull down menu "Please select", "Yes" or "No" for them to select. But they always forgot to select that field and remain "Please select". If i add this VBA code only apply cell is blank. Much appreciate you can advise. Thank you
    • To post as a guest, your comment is unpublished.
      kellytte · 1 years ago
      Hi Happy,
      Just replace the empty value “Sheets("TEST").Range("A1").Value = ""” to the specified text “Sheets("TEST").Range("A1").Value = "Please select"
      And the whole code will be changed as below:

      Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) If Application.Sheets("TEST").Range("A1").Value = "Please select" Then Cancel = True MsgBox "Save cancelled" End If End Sub


  • To post as a guest, your comment is unpublished.
    Happy · 1 years ago
    Hi, I tried above formula which works. May i know is there any formula can force user to fill in before they can save? As i set the pull down menu "Please select", "Yes" or "No" for them to select. But they always forgot to select that field and remain "Please select". If i add this VBA code only apply cell is blank. Much appreciate you can advise. Thank you
  • To post as a guest, your comment is unpublished.
    Am1n · 1 years ago
    Hi
    I have a VBA code that sorts and filters data from one excel table and save 48 different reports on my desktop. but based on those filters, some generated reports have only 1 row (headers) and no data. How can I add some VBA code to my file that prevents to save files that has just one row (header) and no data?
    Thank you
  • To post as a guest, your comment is unpublished.
    Amin · 1 years ago
    Hi
    I have a VBA code that sorts and filters data from one excel table and save 48 different reports on my desktop. but based on those filters, some generated reports have only 1 row (headers) and no data. How can I add some VBA code to my file that prevents to save files that has just one row (header) and no data?
    Thank you
  • To post as a guest, your comment is unpublished.
    Benjamin · 2 years 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 · 2 years 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 · 2 years 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.
        Fatos Gaxha · 6 months ago
        Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

        With Sheets("Sheet1")
        If WorksheetFunction.CountA(.Range("A1:A4")) <> WorksheetFunction.CountA(.Range("B1:C4")) / 2 Then
        Cancel = True
        MsgBox "Please enter a values in columns B and C", vbCritical, "Error!"
        End If
        End With

        End Sub


        Just change the range from a to c, and from b to o and p
        hope it will help
      • To post as a guest, your comment is unpublished.
        Fatos Gaxha · 6 months ago
        Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

        With Sheets("Sheet1")
        If WorksheetFunction.CountA(.Range("A1:A4")) <> WorksheetFunction.CountA(.Range("B1:C4")) / 2 Then
        Cancel = True
        MsgBox "Please enter a values in columns B and C", vbCritical, "Error!"
        End If
        End With

        End Sub


        Just change the range from a to c, and from b to o and p
        hope it will help
        • To post as a guest, your comment is unpublished.
          Reece · 2 months ago
          Do you have a way that this can be coded so that either B or C need to be populated but its not required to populate both?
  • To post as a guest, your comment is unpublished.
    andrewgonzales048@gmail.com · 2 years 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.
    mhoferica@gmail.com · 2 years 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 · 2 years 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 · 2 years 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.
    edusuro · 4 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.
      kelly.extendoffice@gmail.com · 4 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!