Results 1 to 4 of 4
  1. #1
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 1 Time in 1 Post

    Declaring variables (VBA/Excel/2000)

    Confused again
    I am calling a procedure in module B several times from Module A,

    Module A appears in total below.
    ~~~~~~~~~~~~~~~~~~~~~Module A~~~~~~~~~~~~~~~~~~~~~~~~~~~

    <pre>Dim ssubdirlst() As String
    Dim sFileDir() As String
    Dim sFileNames() As String
    Dim dFileDateTime() As Date
    Public Sub test()
    Call SearchDirectory.SearchDirectory("C:My Documents", _
    False, False, ssubdirlst(), sFileDir(), sFileNames(), dFileDateTime())
    Call SearchDirectory.SearchDirectory("C:My Documents", _
    False, False, ssubdirlst(), sFileDir(), sFileNames(), dFileDateTime())
    Call SearchDirectory.SearchDirectory("C:My Documents", _
    False, False, ssubdirlst(), sFileDir(), sFileNames(), dFileDateTime())
    End Sub
    </pre>


    The Declarations of Module B appear below.
    ~~~~~~~~~~~~~~~~~~~~~Module B~~~~~~~~~~~~~~~~~~~~~~~~~~~
    <pre>Option Explicit
    Dim lFileCount As Long
    Dim mstrRoot As String
    Dim lSubDirCount As Long

    Public Sub SearchDirectory(spath As String, JustTopLevel As Boolean, _
    JustDirs As Boolean, sSubDirList() As String, sFileDir() As String, _
    sFileNames() As String, dFileDateTime() As Date)
    </pre>


    Can someone please explain why lFileCount is not reset to 0 on the second and third instance of SearchDirectory being called?
    Regards
    Don

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 28 Times in 28 Posts

    Re: Declaring variables (VBA/Excel/2000)

    You have declared lFilecount at the Module B level. It will keep its value while Module B is loaded, and will only be reset to 0 if you do so explicitly. In particular, it will retain its value between runs of SearchDirectory.

    If you want lFileCount to be reset each time you call SearchDirectory, you must either do so at the beginning of the procedure:

    Dim lFileCount As Long

    Public Sub SearchDirectory(...)
    lFileCount = 0
    ...

    or you must declare lFileCount within the procedure. It will then be initialized to 0 automatically each time you call SearchDirectory:

    Public Sub SearchDirectory(...)
    Dim lFileCount As Long
    ...

  3. #3
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Declaring variables (VBA/Excel/2000)

    Because it is declared (DIMentioned) outside a procedure. This makes it a global variable that is not reset.
    Legare Coleman

  4. #4
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Declaring variables (VBA/Excel/2000)

    Thank you Hans and Legare
    I like to believe that the more I interact with you gentlemen, the less dumb I get.
    Regards
    Don

Posting Permissions

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