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
Prevent Excel from defaulting "True" and "False" to boolean
提问by czchlong
My Excel document defaults all True
and False
strings to TRUE
and FALSE
, which it then treats as a boolean value and this is failing in my macro.
我的 Excel 文档默认 allTrue
和False
strings 为TRUE
and 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 Formula
property (rather than the Value
property) 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 CStr
to change the result of examining the Value
property 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