Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking the values of public variables in Excel VBA - Locals Window Alternative

I have been using the Locals window to check the assignments for procedure level variables.

I have recently updated my code to create a set of public-level variables that read certain inputs from the sheets which do not change from project to project.

When seeking to check these variables, I do not see them in the Locals window, no doubt because they are not locally-defined variables!

Is there an alternative to the Locals Window for Public variables? And if not, how am I supposed to check public variable assignments?

like image 353
psychonomics Avatar asked Feb 22 '13 19:02

psychonomics


People also ask

Which window is similar to the Locals window but it is used to tracing the variables in VBA?

A watch is a variable or expression that has been placed in the window to enable you to monitor its value. Lets you watch the values of variables and expressions as your code executes.

How do I view variables in Excel VBA?

In the VBA editor go to the View menu and click on Locals Window. Each time F8 is pressed one line of VBA is executed. When the range MyRange is set, you can see that values appear for it in the Locals window. By examining the MyRange object you can drill down to see the values in it.

Which window display the entire list of local variables and their current values in VBA?

all of the declared variables in the current scope/procedure with their values and data types.

Which window of VBA debugging display the value of all constants and variables?

The Watch window shows the current values of selected variables or expressions and the current property settings for selected objects. You can use the Watch window to monitor the status of variables and objects as you step through a procedure.


1 Answers

In addition to the Immediate window (as described in the other answer), the Watch window is very helpful in those circumstances. You can activate it in the View menu -> Watch window: enter image description here

Here you can define:

  • Any variable (e.g. your public variables)
  • A full term, e.g. ActiveWorkbook.UsedRange.Address
  • The scope of each watch
  • Even a breakpoint when the value changes or results in True - this is quite handy when debugging, as it allows you quick conditional breakpoints without adding additional code, e.g. if you set a watch to myVar=0 and active Break When Value Is True, the code will automatically stop the moment the potential bug is "initiated"

You can add all of these options in the "Add watch" dialog, which you either get by right clicking on any variable (or other code) in the code module - or by right clicking in the watch window:

enter image description here

In addition, you can simply edit any watch items in the list by double clicking.

Very handy tool for debugging, esp. in combination with Locals and Immediate windows.

like image 145
Peter Albert Avatar answered Oct 14 '22 06:10

Peter Albert