global variables

G

Guest

Hello - When VBA code chokes and the user selects the debug button, all
global variables are cleared. It there a way to keep the global variable
values or an option other than global variables?
 
Z

Zone

I use a separate spreadsheet to store variables I want to keep after
breaking the program. Anyway, I don't think I've ever run into this
problem. If I use this code

Dim myGlobal as string

Sub Huhoh
myGlobal="still here"
msgbox 0/0
End Sub

myGlobal still keeps its string in Debug.

James
 
B

Bob Phillips

He means on a subsequent run the variable is cleared.

--
HTH

Bob Phillips

(remove xxx from email address if mailing direct)
 
G

Guest

Why not write a routine to initialize your global variables. In you code,
have a global variable that is dedicated to just checking

Public gbChecker as Boolean

Initialize it to true in the workbook_open event.


if gbChecker = False then
' run code to init variables
end if


Better is to handle errors so the user never sees an error message displayed
by the system unless it is fatal.
 
G

Guest

There is no way to hold on to the variables short of storing them in a
worksheet somewhere. When the execution is halted the memory that stores the
variables is automatically cleared. In the grander scheme of things if you
just had an error though you can not gurantee that the values in your globals
would be correct anyways so clearing them is really no great loss. What you
want to do is to write bullet proof code with a robust error handler to
gracefully handle anything that is thrown at it. That will keep you from
getting to the debug screen in the first place.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top