Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one restore default case to a variable in VBA (Excel 2010)?

Tags:

At some point I accidentally named a variable range (all lower-case) and since then the Visual Basic for Applications editor refuses to use Range (Pascal-cased); it will auto-correct it to lower-case. How can I reset its "brain" to forget about my past transgression?

NOTE: I've tried doing a global replace-all, but the moment I edit any single line -- it re-corrects once again.

Obviously this is a very very minor, cosmetic issue but watching the R go limp every time I type Range is irritating.

like image 545
Neil C. Obremski Avatar asked Jan 31 '11 15:01

Neil C. Obremski


People also ask

How do I reset a variable in VBA?

You can't "clear" a non-object variable, you can only set it to a defined value. For a string variable, that's usually the empty string "" . For object variables, there is Set myObj = Nothing . Please look below, VARIANT type variable can be set to EMPTY and then checked by IsEmpty() function.

How do you initialize a variable in VBA?

In VBA it is not possible to declare and initialise a variable in one line. You must declare your variable on one line and then assign the value on a new line. Explicitly assigning a value to a local variable after it has been declared is recommended.

How do I assign a value to a variable in Excel VBA?

In VBA, declaring variables is optional, but you can't declare AND set the variable at the same time. We've added a line that assigns the value of a variable. In VBA, just append “. Value”, an equals sign, and the value you want to assign (in quotation marks).


1 Answers

You need to change the name of the variable in a declaration line (Dim statement for example) somewhere (anywhere, actually).

This is one of the most annoying "features" of the VBA IDE (as if version control weren't already hard enough with VBA, the constant case-changing drives me batty). Variables have their case changed globally in the VBA IDE, regardless of the variable's actual scope. The VBA IDE seems to take the approach of most recent declaration wins.

Usually all you need to do is just update the case in a declaration line somewhere, but VBA can be obstinate. I've yet to crack the code completely.

Note: As @Scorchio points out in the comments below, the variable case is changed globally within the current project; other projects that may be open in the VBA IDE (such as other workbooks when working in Excel) are NOT affected.

UPDATE: I expanded on this entry in my Access/VBA blog today: VBA's Case Changing "Feature"

like image 120
mwolfe02 Avatar answered Nov 09 '22 14:11

mwolfe02