Solved

preserve an array to call back later

Posted on 2013-11-06
4
222 Views
Last Modified: 2013-11-06
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
Comment
Question by:fordraiders
  • 2
4 Comments
 
LVL 35

Accepted Solution

by:
Kimputer earned 400 total points
ID: 39628834
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
 
LVL 81

Assisted Solution

by:byundt
byundt earned 100 total points
ID: 39629041
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
 
LVL 3

Author Comment

by:fordraiders
ID: 39629299
so, just by
Global Arr as Variant

This will keep it in memeory to call again later if need be ?
Thanks
0
 
LVL 3

Author Closing Comment

by:fordraiders
ID: 39629331
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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question