Variable with value before being assigned?

J

Jon

When I execute a piece of code (pressing f8 once), and I hover the
cursor over a variable, I find that there is value already assigned.
I can't find this in the declaration section or anywhere else where
this value gets assigned. Does anyone know how this can happen?
 
H

Howard Kaikow

All variables get assigned a default value when a VBA program starts.

However that value might get changed by, say, an Initialize, or other, event
when a Userform loads.
Or by Auto* macros that start when you load Excel, or create worksheets.

You need to search thru all code and ant code that might automatically
execute prior to the statement of interest.
 
D

Dave Peterson

Did you declare it at the top of the module outside any sub/function.

These variables hold there value between code executions. You can see what
happens in a test module:


Option Explicit
Dim iCtr As Long

Sub testme01()
iCtr = iCtr + 1
MsgBox iCtr
End Sub
Sub testme02()
Dim jCtr As Long
jCtr = jCtr + 1
MsgBox jCtr
End Sub


Did you declare the variable as Public in a different module? That makes it
visible to all the modules and it'll also retain the current value between
executions.

Did you declare the variable as static?
Sub testme03()
Static lCtr As Long
lCtr = lCtr + 1
MsgBox lCtr
End Sub

I'm guessing that it's a public variable in a different module.

Hit Ctrl-F (Edit|Find) within the VBE and search for that variable name. But
make sure you check the "current Project" option button.

Then hit Find Next (or F3 when you dismiss that Find dialog).
 

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