What is the difference between .text, .value, and .value2?

  • by

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).

Leave a Reply

Your email address will not be published. Required fields are marked *