Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 254
  • Last Modified:

preserve an array to call back later

Excel vba 2010


After filling a listbox with the  "Arr" array.
Is there a way to preserver it in memory and call it back if needed ?



Dim Arr As Variant
''code.....

With objMyRecordset
                 .ActiveConnection = objMyConn
                 .Open strsql
    
Arr = objMyRecordset.GetRows
       objMyRecordset.MoveFirst
' i need to preserve this Arr and call it back later if needed ?

Open in new window




Thanks
fordraiders
0
Fordraiders
Asked:
Fordraiders
  • 2
2 Solutions
 
KimputerCommented:
Assign Dim Arr as Variant globally, as in, OUTSIDE the sub or function,

Global Arr as Variant

Be sure you're not changing it (Arr) often in other subs or functions, you might get confused about the results (being overwritten somewhere else at unknown times)
0
 
byundtCommented:
From past experience, you will lose the values in your global array from time to time (such as after a fatal error). It is therefore good practice to test whether those values exist, and to renew them if not.

The code below creates an array arr (and displays a message) the first time GlobalArrayTester is run. If that sub is run again, it displays a message saying that the values already exist. If sub MakeAnError is run, then the values stored in memory are lost--and GlobalArrayTester will have to renew them the next time it runs.
Dim arr As Variant

Sub GlobalArrayTester()
If IsEmpty(arr) Then
    arr = Worksheets(1).Range("A1:A5").Value
    MsgBox "arr must be created"
Else
    MsgBox "arr still exists " & arr(4, 1)
End If
End Sub

Sub MakeAnError()
Dim ws As Worksheet
Set ws = Worksheets("abc")  'This worksheet doesn't exist, and so will cause run-time error
End Sub

Open in new window

0
 
FordraidersAuthor Commented:
so, just by
Global Arr as Variant

This will keep it in memeory to call again later if need be ?
Thanks
0
 
FordraidersAuthor Commented:
worked great Thanks !
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now