ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Variable with value before being assigned? (https://www.excelbanter.com/excel-programming/296146-variable-value-before-being-assigned.html)

Jon[_16_]

Variable with value before being assigned?
 
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?

Howard Kaikow

Variable with value before being assigned?
 
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.

--
http://www.standards.com/; See Howard Kaikow's web site.
"Jon" wrote in message
om...
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?




Dave Peterson[_3_]

Variable with value before being assigned?
 
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).


Jon wrote:

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?


--

Dave Peterson



All times are GMT +1. The time now is 08:56 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com