vba 根据其他 2 个单元格的值,强制设置一个 excel 单元格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22660342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 02:29:23  来源:igfitidea点击:

Make an excel cell mandatory, based on value of other 2 cells

excelexcel-vbavba

提问by user1621796

So.. I've an excel where 2 consecutive cells can be marked with the same value (mostly Text), based on this 2 cells the value of the 3rd cell should be filled mandatorily by the user.

所以..我有一个 excel,其中 2 个连续的单元格可以标记为相同的值(主要是文本),基于这 2 个单元格,用户应该强制填充第 3 个单元格的值。

The range of cells can be different as I need to use the same snippet cross many excels.

单元格的范围可以不同,因为我需要在许多 excel 中使用相同的代码段。

Any help would be sincerely appreciated.

任何帮助将不胜感激。

Many thanks! :)

非常感谢!:)

回答by sten

There's more then one way to skin a cat, but probably the easiest way to do it would be to include a vba sub which (on some trigger, like submitting) checks the value of cell 1 and 2 and IF that value is whatever you want to trigger on... if cell 3 is blank prompt the user to fill in box three.

给猫剥皮的方法不止一种,但最简单的方法可能是包含一个 vba sub,它(在某些触发器上,例如提交)检查单元格 1 和 2 的值,如果该值是您想要的任何值触发... 如果单元格 3 为空白,则提示用户填写框 3。

IF Range("A1").Value == "Trigger" And Range("A2").Value == "Trigger" And Range("A3") == "" Then msgBox("You must fill in cell A3")

If you add a break statement like End Sub, the sheet can't continue until that cell is filled.

如果添加像 End Sub 这样的 break 语句,则在填充该单元格之前,工作表无法继续。

回答by Gary's Student

This is an example for A1, B1, C1enter the following event macro in the worksheet code area:

这是A1B1C1的示例, 在工作表代码区域中输入以下事件宏:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim A As Range, B As Range, C As Range
    Set A = Range("A1")
    Set B = Range("B1")
    Set C = Range("C1")
    If A <> "" And B <> "" And C = "" Then
        Application.EnableEvents = False
            C.Select
        Application.EnableEvents = True
        MsgBox "please enter a value in cell C1"
    End If
End Sub

Because it is worksheet code, it is very easy to install and automatic to use:

因为是工作表代码,所以很容易安装,自动使用:

  1. right-click the tab name near the bottom of the Excel window
  2. select View Code - this brings up a VBE window
  3. paste the stuff in and close the VBE window
  1. 右键单击 Excel 窗口底部附近的选项卡名称
  2. 选择查看代码 - 这会打开一个 VBE 窗口
  3. 粘贴内容并关闭 VBE 窗口

If you have any concerns, first try it on a trial worksheet.

如果您有任何疑虑,请先在试用工作表上尝试。

If you save the workbook, the macro will be saved with it. If you are using a version of Excel later then 2003, you must save the file as .xlsm rather than .xlsx

如果您保存工作簿,宏将与它一起保存。如果您使用的是 2003 年以后的 Excel 版本,则必须将文件另存为 .xlsm 而不是 .xlsx

To remove the macro:

要删除宏:

  1. bring up the VBE windows as above
  2. clear the code out
  3. close the VBE window
  1. 调出如上的 VBE 窗口
  2. 清除代码
  3. 关闭 VBE 窗口

To learn more about macros in general, see:

要了解有关一般宏的更多信息,请参阅:

http://www.mvps.org/dmcritchie/excel/getstarted.htm

http://www.mvps.org/dmcritchie/excel/getstarted.htm

and

http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx

http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx

To learn more about Event Macros (worksheet code), see:

要了解有关事件宏(工作表代码)的更多信息,请参阅:

http://www.mvps.org/dmcritchie/excel/event.htm

http://www.mvps.org/dmcritchie/excel/event.htm

Macros must be enabled for this to work!

必须启用宏才能使其工作!