View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Jim Cone[_2_] Jim Cone[_2_] is offline
external usenet poster
 
Posts: 1,549
Default Save Array in memory

You are loading it into memory: the data() array is declared as Public outside of any sub
or function.
So all you have to do is check the bounds of the array to see if has been dimensioned
(filled).
The following will work for your particular circumstance but is not a universal
solution...
'---
Option Explicit
Public Const Delim As String = " ¿ "
Public Const hil As String = "Best regards from Joergen"
Public data() As Variant

Sub Form()
Dim Uniqs As Collection
Dim Countarr As Long
Dim sh As Worksheet
Dim RRange As Range
Dim cell As Range

'Verify array
On Error Resume Next
Countarr = UBound(data(), 1)
On Error GoTo 0

If Countarr < 1 Then ' if zero then fill array
Set sh = Sheets(2)
Set RRange = sh.Range("A1:A" & _
sh.Cells(sh.Rows.Count, 1).End(xlUp).Row)
Set Uniqs = New Collection
For Each cell In RRange
On Error Resume Next
Uniqs.Add cell.Value, CStr(cell.Value)
If Err = 0 Then
Countarr = Countarr + 1
ReDim Preserve data(1 To Countarr)
data(Countarr) = cell.Value & Delim _
& cell.Offset(0, 1) & Delim & cell.Offset(0, 2)
End If
On Error GoTo 0
Next cell
End If

Set cell = Nothing
Set RRange = Nothing
Set sh = Nothing
Set Uniqs = Nothing
UF.Show
End Sub
'---
Jim Cone
Portland, Oregon USA
http://www.mediafire.com/PrimitiveSoftware
("Lottery Numbers" workbook - in the free folder)




"Jorgen Bondesen"
wrote in message
...
Hi NG

I'm using macro below.
Each time I'm opening my User Form = UF, I must run this macro for getting array:
data()
Is there a way I can load the Array [data()] in memory, so I only run the macro when
opening the file and Erase data() when closing the file?


Option Explicit

Public Const Delim As String = " ¿ "
Public Const hil As String = "Best regards from Joergen"

'// Array for form
Public data() As Variant

'----------------------------------------------------------
' Procedure : Form
' Date : 20110227
' Author : Joergen Bondesen
' Modifyed by :
' Purpose :
' Note :
'----------------------------------------------------------
'
Sub Form()
Dim sh As Worksheet
Set sh = Sheets(2)

Dim RRange As Range
Set RRange = sh.Range("A1:A" & _
sh.Cells(sh.Rows.Count, 1).End(xlUp).Row)

Dim Countarr As Long
Countarr = 0

Dim cell As Range
For Each cell In RRange
Dim Uniqs As New Collection
On Error Resume Next
Uniqs.Add cell.Value, CStr(cell.Value)
If Err = 0 Then
Countarr = Countarr + 1
ReDim Preserve data(1 To Countarr)
data(Countarr) = cell.Value & Delim _
& cell.Offset(0, 1) & Delim & cell.Offset(0, 2)
End If
On Error GoTo 0
Next cell

Load UF
UF.Show

Set sh = Nothing
Set RRange = Nothing
End Sub


--
Best Regards
Joergen Bondesen