使用 VBA 以编程方式忽略 Excel 中的“数字存储为文本”错误

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

Ignore 'Number Stored as Text' Error in Excel programmatically using VBA

excelexcel-vbaerror-handlingvba

提问by Foreever

I want to store text such as '001234' in a cell. I have set the number format of this cell to text. After storing, an error 'Number Stored as Text' is shown in the form of a green triangle at the top left corner of the cell. It is very disturbing and I want it to be removed programmatically.

我想在单元格中存储诸如“001234”之类的文本。我已将此单元格的数字格式设置为文本。存储后,单元格左上角以绿色三角形的形式显示错误“数字存储为文本”。这非常令人不安,我希望以编程方式将其删除。

回答by Gary's Student

Consider:

考虑:

Sub Macro1()
    Application.ErrorCheckingOptions.NumberAsText = False
End Sub

This is a single line of a much larger macro to configure Excel when I begin working on a new computer.

这是当我开始在新计算机上工作时配置 Excel 的更大宏的单行。

回答by user6468957

You can select the range you want to work and then put for example (now added speechmarks):

您可以选择要工作的范围,然后输入例如(现在添加了语音标记):

Range("A1:Z20").Application.ErrorCheckingOptions.NumberAsText = False

回答by James Hammontree

The above examples turn off the "number stored as text" checking for the application.

上面的示例关闭了应用程序的“数字存储为文本”检查。

This code will turn it off for a particular cell:

此代码将为特定单元格关闭它:

range("G93").Errors.Item(xlnumberastext).Ignore = True

回答by tigeravatar

Alternately, instead of formatting the cell as text, you could format the cell as 000000 to get the leading 0's to show.

或者,您可以将单元格格式化为 000000 以显示前导 0,而不是将单元格格式化为文本。