vba 如何在单元格范围内设置 0 默认值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11672456/
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-11 17:03:50  来源:igfitidea点击:

How to set a 0 default value within a range of cells?

excel-vbaexcel-2010vbaexcel

提问by JSchwartz

I am using a range of cells (columns F-G-H) for estimation purposes, and having an estimate of 0 hours is acceptable, however blank/empty is not. Seeing as users can add rows as they see fit I want to ensure that all cells in columns (F-G-H) except for the header (row1) default to 0 (and the user can change / overwrite it) if needed.

我正在使用一系列单元格(FGH 列)进行估计,估计 0 小时是可以接受的,但空白/空不是。看到用户可以添加他们认为合适的行,我想确保列 (FGH) 中除标题 (row1) 之外的所有单元格默认为 0(如果需要,用户可以更改/覆盖它)。

Is there a way I can enforce this for newly created rows for this specific range? I know I can use a formula in the existing cells but that doesn't help for rows that are user-created after the fact.

有没有办法可以为这个特定范围的新创建的行强制执行这个?我知道我可以在现有单元格中使用公式,但这对事后用户创建的行没有帮助。

(BTW - simularily for column C I want to default the cell to "WI-" and for column D I want to default the cell to "REQ-", etc...).

(顺便说一句 - 同样对于列 CI 想要将单元格默认为“WI-”,对于列 DI 想要将单元格默认为“REQ-”等......)。

Any help/hints/ideas would be much appreciated.
Thanks,

任何帮助/提示/想法将不胜感激。
谢谢,

采纳答案by Siddharth Rout

Do this.

做这个。

  1. Enter the number 1 in an unused cell
  2. Copy that cell.
  3. Highlight the cells in which you want blanks replaced by zeros.
  4. Choose Edit > Paste Special from the menu.
  5. In the Operation section of the dialog box select Multiply and click on OK.
  6. Numeric cells won't change and Cells containing any text data will be ignored.
  7. All blank cells will now contain a zero.
  1. 在未使用的单元格中输入数字 1
  2. 复制那个单元格。
  3. 突出显示要在其中用零替换空白的单元格。
  4. 从菜单中选择“编辑”>“选择性粘贴”。
  5. 在对话框的操作部分选择乘法并单击确定。
  6. 数字单元格不会改变,包含任何文本数据的单元格将被忽略。
  7. 所有空白单元格现在都将包含一个零。

For this to apply to newly added rows, use the Worksheet_Changeevent in which you will predefine your range where this will be applicable. Simply record a macro for the above steps and use that code in the Worksheet_Changeevent.

要将此应用于新添加的行,请使用Worksheet_Change您将在其中预定义适用范围的事件。只需为上述步骤记录一个宏并在Worksheet_Change事件中使用该代码。

Also remember to use proper error handling and set Application.EnableEvents = Falseand reset it again in the error handler.

还要记住使用正确的错误处理并Application.EnableEvents = False在错误处理程序中再次设置和重置它。

(BTW - simularily for column C I want to default the cell to "WI-" and for column D I want to default the cell to "REQ-", etc...).

(顺便说一句 - 同样对于列 CI 想要将单元格默认为“WI-”,对于列 DI 想要将单元格默认为“REQ-”等......)。

For this the above method will not work. You will have to handle that in VBA

为此,上述方法将不起作用。你将不得不在 VBA 中处理它