Results 1 to 2 of 2

Thread: global variable

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2010
    Posts
    18

    global variable

    Hi,

    I just wanted to be sure that the value of a global variable in VBA is lost when an error is produced. There is not way to solve this problem?? Then , it is not good to use this kind of variables??

    Thank you

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: global variable

    the variable may have nothing to do with the error,
    the value of the global variables are retained, you should use proper error handling
    vb Code:
    1. On Error GoTo errh
    2. vtest = "whynot"  'public global variable
    3. Err.Raise 99
    4. MsgBox "will not show"
    5. Exit Sub
    6. errh:
    7. vtest = vbNullString
    if you comment out the on error goto, excel will break, but the variable will still retain value
    Last edited by westconn1; Mar 21st, 2010 at 05:57 AM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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



Click Here to Expand Forum to Full Width