variable not reaching the innner sub

Results 1 to 3 of 3

Thread: variable not reaching the innner sub

  1. #1
    Jason Guest

    Default variable not reaching the innner sub

    Hi<BR><BR>This may be a little bit of a clueless question but as far as I have gathered variables declared and defined outside of a subroutine should be able to be used within a sub without having to pass the variable to the sub as a parameter.<BR><BR>Or am I losing it here ?<BR><BR>E.G.<BR><BR>Dim myVar1, myVar2<BR>myVar1 = 35<BR>myVar2 = 22<BR><BR>Sub Adder(firstVal)<BR> firstVal = firstVal + myVar2<BR>End Sub<BR><BR>This is an oversimplified version of what I am trying to do but it seems that the value "myVar2" is not accessable within the sub until I pass it to the sub as a parameter.<BR><BR>If I want to use a bunch of variables in a sub/function it seems tedious if I have to pass every single variable to it that I want to use, rather than been able to use it straight if the variable exists in the file the sub is defined in.<BR><BR>TIA and apologies for the rather clueless question.

  2. #2
    Join Date
    Dec 1969

    Default RE: variable not reaching the innner sub

    You should be able to refer to a global variable in any sub or function - exactly as you&#039re trying to. Some ideas as to why it&#039s not working:<BR><BR>1. Typos - the most common. Make sure you&#039ve used OPTION EXPLICIT to force variable declaration - this will catch typos.<BR>2. Naming collision - are you sure that you haven&#039t reset this variable somewhere else?<BR>3. Not actaully global - is the variable defined within another sub or function? If it is, then it&#039s only available within that sub or function.<BR><BR>MY TIP: I always try to minimise the use of global variables - they make debugging a nightmare. It may be a little more tedious to pass parameters, but it makes it a lot easier to trace what&#039s happening. You&#039ll save time (and stress) in the end.<BR><BR>Dunc

  3. #3
    Jason Guest

    Default RE: variable not reaching the innner sub

    Okay it works now after much fiddling...<BR><BR>Thx I am gonig to try get out of the habit of using globals of which mine are mostly...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts