跳到主要内容

在关闭工作簿之前如何使特定单元格强制输入?

例如,当您与其他用户共享工作簿以进行需要实名注册的调查时,每个接受调查的用户都需要在B1中输入其姓名。 但是在某些情况下,某些用户可能在调查后关闭工作簿而不输入其名称。 在本文中,我介绍了VBA,以在关闭工作簿之前将特定的单元格强制化。

使用VBA将单元格强制输入


箭头蓝色右气泡 使用VBA将单元格强制输入

1.启用包含必需单元格的工作簿,然后按 Alt + F11键 打开钥匙 Microsoft Visual Basic应用程序 窗口。

2.在 项目 窗格,双击 的ThisWorkbook,然后选择 工作簿收盘前 从右侧部分列表中,然后将以下代码粘贴到脚本中。 看截图:

VBA:强制设置单元格

    If Cells(1, 2).Value = "" Then
        MsgBox "Cell B1 requires user input", vbInformation, "Kutools for Excel"
        Cancel = True
    End If

doc mandantory输入1

3.然后保存此代码并关闭此弹出窗口。 现在,如果在关闭工作簿时B1单元格为空白,则会弹出以下对话框,提醒您在B1中输入内容。 看截图:
doc mandantory输入2

Tips::您可以将单元格B1更改为所需的其他单元格。

最佳办公生产力工具

🤖 Kutools 人工智能助手:基于以下内容彻底改变数据分析: 智能执行   |  生成代码  |  创建自定义公式  |  分析数据并生成图表  |  调用 Kutools 函数...
热门特色: 查找、突出显示或识别重复项   |  删除空白行   |  合并列或单元格而不丢失数据   |   不使用公式进行四舍五入 ...
超级查询: 多条件VLookup    多值VLookup  |   跨多个工作表的 VLookup   |   模糊查询 ....
高级下拉列表: 快速创建下拉列表   |  依赖下拉列表   |  多选下拉列表 ....
列管理器: 添加特定数量的列  |  移动列  |  切换隐藏列的可见性状态  |  比较范围和列 ...
特色功能: 网格焦点   |  设计图   |   大方程式酒吧    工作簿和工作表管理器   |  资源库 (自动文本)   |  日期选择器   |  合并工作表   |  加密/解密单元格    按列表发送电子邮件   |  超级筛选   |   特殊过滤器 (过滤粗体/斜体/删除线...)...
前 15 个工具集12 文本 工具 (添加文本, 删除字符,...)   |   50+ 图表 类型 (甘特图,...)   |   40+ 实用 公式 (根据生日计算年龄,...)   |   19 插入 工具 (插入二维码, 从路径插入图片,...)   |   12 转化 工具 (小写金额转大写, 货币兑换,...)   |   7 合并与拆分 工具 (高级组合行, 分裂细胞,...)   |   ... 和更多

使用 Kutools for Excel 增强您的 Excel 技能,体验前所未有的效率。 Kutools for Excel 提供了 300 多种高级功能来提高生产力并节省时间。  单击此处获取您最需要的功能...

产品描述


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

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,发布者,Access,Visio和Project。
  • 在同一窗口的新选项卡中而不是在新窗口中打开并创建多个文档。
  • 每天将您的工作效率提高50%,并减少数百次鼠标单击!
Comments (31)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
How would I do this if I am wanting to to apply to the entire workbook with the exception of a few columns. I have columns A:AA and currently have 4,527 rows but more will be added and I will need to make sure the new rows have the same requirement. I need all fields to be requires with the exception of the below.

• Artemis (Column A)
• Host Access Location (Column H)
• Creation Tool (Synthesia) (Column R)
• Search Terms (Column U)
• Notes (Column V)
• eReview SME (column Z)
• Voice Talent (column AA)

Can you please assist in how I can set this up and include any future rows a added?
This comment was minimized by the moderator on the site
How would I do this if I am wanting to to apply to the entire workbook with the exception of a few columns. I have columns A:AA and currently have 4,527 rows but more will be added and I will need to make sure the new rows have the same requirement. I need all fields to be requires with the exception of the below.

• Artemis (Column A)
• Host Access Location (Column H)
• Creation Tool (Synthesia) (Column R)
• Search Terms (Column U)
• Notes (Column V)
• eReview SME (column Z)
• Voice Talent (column AA)

Can you please assist in how I can set this up and include any future rows a added?
This comment was minimized by the moderator on the site
Sorry Sir,

For somebody using VBA for first time, can you please provide some guidance on how to initiate this? I want each row to have same feature I.E. (until one completes all info on row)
This comment was minimized by the moderator on the site
Hi, PM, just follow the steps in this article, then replace the code with the code I provided for you, and then save it. And when you share the workbook to others whose username are different with you, they will be asked to fill every cell in the range (a1:c3, you can change it in the code) until they fill all.
This comment was minimized by the moderator on the site
Thank you so very much, apologies I did not reply back.
This comment was minimized by the moderator on the site
Do mention it. I am glad that can help you.
This comment was minimized by the moderator on the site
Can I email you document so you can best advise me how to implement VBA code so file cannot save if each row is not fully populated? Mine is a bit tricky. Will appreciate the help, been trying for so long.
This comment was minimized by the moderator on the site
Hi, here is a code, it will pops a dialog to remind the user which cell is needed entered, and the workbook cannot be closed and saved until all cells in the range a1:c3 are all filled with contents.
Please modify the script in the code:
1. change the range as you need,
2. change the the user name "Kutools for Excel" in the code to your own username (please make sure other user has no the same user name with you)

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    
    If Application.UserName <> "Kutools for Excel" Then

        Set Rg = Sheets("Sheet2").Range("a1:c3")

        If WorksheetFunction.CountBlank(Rg) > 0 Then

            MsgBox Rg.Parent.Name & " " & Rg.Address & " requires user input", vbInformation, "Kutools for Excel"
            Cancel = True
        End If
    End If

End Sub
This comment was minimized by the moderator on the site
Is het mogelijk om als regel erin te zetten dat als C1 een bepaalde tekst bevat dan D1 ook ingevuld moet worden. Dit moet gelden voor circa 2.000 regels. Het bestand wordt elke keer aangevuld dus het moet alleen gelden voor het aantal regels die nu zijn ingevuld.

Wanneer ik volgende functie erin zet werkt het niet zoals ik wil omdat ik dan meldingen krijg van cellen die nog aangevuld moeten worden en ik wil een tekst als waarde en geen cijfer.

Als Application.WorksheetFunction.CountA(Range("B16:B300")) > 0 then

If Application.WorksheetFunction.CountA(Range("D16:D300")) <> Range("D16:D300").Count Then

MsgBox "Cell D16:D300 vereist gebruikersinvoer", vbInformation, "Kutools for Excel"

Annuleren = Waar

End If

End If

Gr emma
This comment was minimized by the moderator on the site
Is it possible to create a code in a new generated workbook? the scenario is, I will generate a new workbook, and that generated workbook must have a mandatory field to be filled, the user cannot save unless it is not filled the mandatory field. I have already created, generated a new workbook. my problem is, the new generated workbook does not have a code.
This comment was minimized by the moderator on the site
Question, the code worked, but how can choose a range from A7:M7 up to the last row?

and whenever the user will save or close the workbook, the mandatory field have to be filled out
This comment was minimized by the moderator on the site
Hi, Lester, here is a code we modified may help you, change the range A2:F2 to the range as you need.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
'UpdatebyExtend20220916
Dim xRgCount As Integer

Dim xURg As Range

Dim xStr As String

Dim xWSh As Worksheet

Dim xRg, xRg1, xRg2, xRg3 As Range

xStr = "A2:F2"

 

Set xWSh = Application.ActiveSheet

Set xRg = xWSh.Range(xStr)

Set xURg = xWSh.UsedRange

Set xRg1 = xWSh.Cells.Item(xRg.Row + xRg.Rows.Count, 1)

Set xRg2 = xURg.Item(xURg.Count)

Set xRg3 = xWSh.Range(xRg1, xRg2)

Debug.Print xRg3.Address

If (xURg.Row + xURg.Rows.Count - 1) > (xRg.Row + xRg.Rows.Count - 1) Then

   If Application.WorksheetFunction.CountA(xRg3) <> xRg3.Count Then

    MsgBox "There are blank cells under range A2:F2", vbInformation, "Kutools for Excel"

Cancel = True

   End If

End If

End Sub


This comment was minimized by the moderator on the site
Is there a way to make a cells input mandatory before an Active X command button can be used? I have a command button setup to submit a form via email but want to make certain cells mandatory before the button will submit the email.
This comment was minimized by the moderator on the site
Hi, Craig, sorry this problem cannot be solved here, maybe other else can help you.
This comment was minimized by the moderator on the site
i want to make the tick marks mandatory in sheet
This comment was minimized by the moderator on the site
I want to make rows C2 to C7 mandatory and C13 to C19 mandatory?

Any help?

2 sets of ranges are here but under the same column.
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations