.text,.value和.value2有什么区别? [英] What is the difference between .text, .value, and .value2?

查看:401
本文介绍了.text,.value和.value2有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是要求任何脚本的帮助,但我的问题是澄清。最近我在Excel中做了很多VB脚本,所以我在这个问题上真的是指Excel。 .text,.value和.value2有什么区别?喜欢什么时候应该使用target.text,target.value和target.value2?我从来没有使用value2选项,但仍然想知道它是用于什么。



有时如果我使用.text它给我一个错误,我需要使用。当我只是检查或操纵单元格中的文本时的价值。然后有时候,当我认为我应该使用.value我得到一个错误,我需要使用.text。通常它接受或没有问题,但有时它会有所作为。我知道这一点必须有一些逻辑,但我似乎无法弄清楚。



我还发现,如果你只是把它当作目标没有指定.text或.value它最初会起作用,但是有些人会做的事最终会导致脚本出错,所以最好使用一个.something。我想我问的是,如果有人可以给我一些指导方针,经验法则,如何正确使用每一个,何时需要使用。



<感谢您的解释。我好好理解一下他们都是很好的解释。下面是我的一些代码的一个小例子。我以为应该是target.text,但是当我使用target.value它有效时会出错。

 如果LCase( Target.Value)= LCase(HLO)和Target.Column = 15然后
Target.Value =高级别中断
结束如果

我仍然有点困惑,因为当我想到价值或价值2,特别是在你提供的答案之后,我认为他们应该只用于数字。然而,在我的例子中,我正在谈论严格的文本,这是我的脚本引用的很多(单元格中的文本,比数字更多)。

解决方案

.Text 给你一个字符串,表示在单元格屏幕上显示的内容。使用.Text通常是一个坏主意,因为你可以得到####



.Value2 给你底层单元格的值(可以为空,字符串,错误,数字(双)或布尔值)



.Value 给你一样的.Value2,除非这个单元被格式化为货币或日期,它给你一个VBA货币(可能会截断小数位)或VBA日期。



使用.Value或.Text通常是一个坏主意,因为您可能无法从单元格中获得真正的价值,而且它们比.Value2



更广泛的讨论见我的文本与价值vs Value2


I am not asking for help with any script, but my question is for clarification. Lately I have been doing a lot of VB scripting in Excel, so I am really referring to Excel in this question. What is the difference between .text, .value, and .value2? Like when should I use target.text, target.value, and target.value2? I never used the value2 option but would still like to know what it is used for.

Sometimes if I use .text it gives me an error and I need to use .value when I am only checking or manipulating the text within a cell. Then sometimes when I think I should use .value I get an error and I need to use .text. Usually it accepts either or without an issue, but sometimes it does make a difference. I know there has to be some logic to this, but I can’t seem to figure it out.

I also found out that if you just leave it as target without specifying .text or .value it will initially work, but then something that someone does will eventually cause the script to error, so it is always best to use a .something on it. I guess what I am asking is if someone can give me some sort of guideline, rule of thumb, as to how to properly use each one and when it needs to be used.

Thanks for the explanation guys. I kind of understand it better. They are both good explanations. Below is a tiny example of some of my code that does work. I thought it should be target.text, but it would error so when I used target.value it worked.

If LCase(Target.Value) = LCase("HLO") And Target.Column = 15 Then
    Target.Value = "Higher Level Outage"
End If

I am still slightly confused because when I think of value or value2, especially after your answers that you provided, I think they should only be used for numbers. However, in my example I am talking about strictly text, which is a lot of what my script refers to (text in the cells, more so than numbers).

解决方案

.Text gives you a string representing what is displayed on the screen for the cell. Using .Text is usually a bad idea because you could get ####

.Value2 gives you the underlying value of the cell (could be empty, string, error, number (double) or boolean)

.Value gives you the same as .Value2 except if the cell was formatted as currency or date it gives you a VBA currency (which may truncate decimal places) or VBA date.

Using .Value or .Text is usually a bad idea because you may not get the real value from the cell, and they are slower than .Value2

For a more extensive discussion see my Text vs Value vs Value2

这篇关于.text,.value和.value2有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆