vba 防止 Excel 将“True”和“False”默认为布尔值

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

Prevent Excel from defaulting "True" and "False" to boolean

excelvba

提问by czchlong

My Excel document defaults all Trueand Falsestrings to TRUEand FALSE, which it then treats as a boolean value and this is failing in my macro.

我的 Excel 文档默认 allTrueFalsestrings 为TRUEand FALSE,然后将其视为布尔值,这在我的宏中失败。

I can right click -> format cells -> select text, but I don't know which cells users will use, so this method will not work.

我可以right click -> format cells -> select text,但我不知道用户将使用哪些单元格,因此此方法不起作用。

Is there a global method to disable this automatic conversion or perhaps a method using VBA?

是否有禁用此自动转换的全局方法或使用 VBA 的方法?

采纳答案by czchlong

Solved the problem through another method and just formatted all cells as Text

通过另一种方法解决了问题,并将所有单元格格式化为 Text

回答by PowerUser

Make a second column. Use =IF(A1,"T","F")(where A1 is the boolean value) and use the new column instead.

制作第二列。使用=IF(A1,"T","F")(其中 A1 是布尔值)并改用新列。

I don't know how your macro works, but you might be able to use that in your macro instead.

我不知道你的宏是如何工作的,但你可以在你的宏中使用它。

回答by barrowc

If there is no actual formula in the cell then examine the Formulaproperty (rather than the Valueproperty) and you should get a String containing "True" or "False" as appropriate

如果单元格中没有实际公式,则检查Formula属性(而不是Value属性),您应该得到一个包含“True”或“False”的字符串。

If there is a formula in the cell then use CStrto change the result of examining the Valueproperty from a Boolean value to a String value - see http://msdn.microsoft.com/en-us/library/gg264697.aspx

如果单元格中有公式,则用于CStr将检查Value属性的结果从布尔值更改为字符串值 - 请参阅http://msdn.microsoft.com/en-us/library/gg264697.aspx