2

I no longer use Hungarian notation when coding VBA but I sometimes run into situations where the type of a given variable isn't obvious.

For example, take a variable with a name of dataRow. It could be a row number with a Long variable type. It could be a Range variable representing an entire row. It could be a ListRow in an Excel table. It could also be the Range of an Excel ListRow...and so on.

Is there some way to quickly look this up the the MS Office VBA editor? Can I somehow do this with variable naming without using variable names? I am not looking for opinions on the pros and cons of Hungarian notation, just how to see variable types without it.

Community
  • 1
  • 1
ChrisB
  • 3,024
  • 5
  • 35
  • 61
  • View -> Locals Window – 0m3r Apr 21 '18 at 01:08
  • @0m3r that's helpful while the code is running, but otherwise the Locals window is empty. – ChrisB Apr 21 '18 at 01:11
  • Are you explicitly declaring all variables? While I've seen it said that variables should be declared immediately before their first use, for your purposes it may be better to declare them all at the top of the procedure. Copy the declarations into a basic text editor and refer to it as required. – Mark Fitzgerald Apr 21 '18 at 02:32
  • @Mark Fitzgerald, I use `Option Explicit` and declare variables explicitly (`as long`, `as Variant`, etc.) I tend to declare them declare them immediately before their first use, or if used in a loop, before the loop. – ChrisB Apr 24 '18 at 16:48

1 Answers1

4

If you right-click on the variable name in question and select Quick Info, you will get a pop-up with the variable type. The pop-up also specifies if the variable is a local variable or an argument.

A screenshot from Excel's VBA editor. A Sub called foo is displayed which has a local variable called i which is an Integer. A yellow pop-up is displayed saying "Local i As Integer"

Variables from other scopes seem to just show the variable name and type. Constants don't indicate a variable type - they just give the constant name and value - e.g. "z = -1" but the value shown can be incorrect.

barrowc
  • 10,444
  • 1
  • 40
  • 53