+ Reply to Thread
Results 1 to 2 of 2

having trouble with dim variable staying last value when coming back from sub procedure?

Hybrid View

  1. #1
    Registered User
    Join Date
    08-26-2010
    Location
    Richmond Hill, Canada
    MS-Off Ver
    Excel 2003
    Posts
    5

    Question having trouble with dim variable staying last value when coming back from sub procedure?

    Hi,
    My object oriented skills are lacking so I can't figure this variable problem out.

    Here is the simplified code and output.

    dim x as integer
    dim y as integer

    x = 1
    xfixed = x

    do

    call dothiswork(x)
    " inside dothiswork procedure the value x passed into it can increment to a number, for example 20)

    when I come out of this procedure the value for x = 20 and now it will pass into the next procedure (dothisotherwork) as 20 when I
    really want is to pass in original value 1 for x.

    call dothisotherwork (xfixed)
    so before calling dothisotherwork I create a xfixed variable and assign it value of x.

    y = y + 1
    loop until y = 2

    the real code should be

    do

    dothiswork(x)
    dothisotherwork(xfixed)
    y = y +1

    loop until y =2

    this xfixed variable nonsense is annoying me and making my code look horrible as my real code is nested do loops and significantly more sophisticated.

    I know I'm missing something. I have followed my X very closely it is not a public variable. Can this be fixed with private declaration at module level or something?

    any help would be greatly appreciated.

    thank you

    Chris

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: having trouble with dim variable staying last value when coming back from sub procedur

    dothisotherwork may receiving the variable ByRef(by default).


    Here's a good example

    http://www.cpearson.com/excel/byrefbyval.aspx

    Please use a code tag for vba code.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. VLOOKUP Formula coming back with NA#
    By monza2 in forum Excel Formulas & Functions
    Replies: 11
    Last Post: 10-18-2012, 01:38 AM
  2. emailed docs coming back in different format
    By JL Adamson in forum Excel General
    Replies: 0
    Last Post: 10-05-2005, 12:05 PM
  3. How to pass a variable from Form back to calling procedure
    By John in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-21-2005, 11:05 AM
  4. Values assigned to variables staying in memory when procedure stop
    By Peter Rooney in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-12-2005, 11:05 AM
  5. why do my deleted emails keep coming back
    By denise@shelteredcovemarina.com in forum Excel General
    Replies: 1
    Last Post: 06-03-2005, 02:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1