Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

debugging - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...