使工作簿上的特定工作表必须保留 [英] Making fields mandatory of a specific sheet on workbook save

查看:87
本文介绍了使工作簿上的特定工作表必须保留的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用excel中的宏使excel工作簿中的字段成为必需的。但是,问题是工作簿包含多个工作表,宏适用于所有工作表。

I am using macros in excel to make fields mandatory in excel workbook. However, the problem is that the workbook contains multiple worksheets and the macro applies to all the worksheets.

有没有办法在工作簿中定位特定的工作表?

Is there a way to target a specific worksheet in the workbook?

以下是我正在使用的代码:

Below is the code i am using:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Cells(5, 2).Value = "" Then
    MsgBox "Please fill cell B5"
    Cancel = True
End If
End Sub


推荐答案

请尝试:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
  For i = 1 To ThisWorkbook.Sheets.Count 
   If ThisWorkbook.Sheets(i).Name = "Name of specific sheet" Then
     If ThisWorkbook.Sheets(i).Cells(5, 2).Value = "" Then
        MsgBox "Please fill cell B5"
        Cancel = True
     End If
   End If
  Next i
End Sub

哪里特定工作表的名称是您验证的工作表名称单元格(5,2)。瓦卢e =

Where the "Name of specific sheet" is the worksheet name that you validate Cells(5, 2).Value = "" for.

这篇关于使工作簿上的特定工作表必须保留的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆