Hi Dave,
I decided to make a clear short program with one procedure call. This
uses the dictionary and dynamic array assignments. I added the erase feature
to insure the previous array was eliminated entirely of all values.
I also placed a random procedure/function in this program to allow
different size arrays for everything you would want, such as variable size
sets to save.
At the end all these sets are saved in there defined name and done in
the SaveSets procedure at the bottom.
In that SaveSets procedure I also added a msgBox to let you know the
size of each set as it is being saved. This size is calculated using the
UBound or Upper Bound of your array function. Note you have to specify what
array you are doing so I placed the dictionary name value in to do the
calculation.
Bruce
Variable Set Length Program:
' Make sure you have all variables defined:
Option Explicit
' Initialize all variables and arrays being used:
Dim ArrayOfSets(), SetArray(), intSetCount, intSetSize, intSetCounter,
intCount
' Init number of sets using 10 as max:
Randomize
intSetCount = Int( 10 * Rnd + 1)
' Init number of set elements using 20 as max:
Randomize
intSetSize = Int( 20 * Rnd + 1)
'Set initial size of array to store all sets:
Erase arrayOfSets
Redim arrayOfSets( intSetCount)
' Set up the dictionary of names for each set:
Dim SetNamesDict
Set SetNamesDict = CreateObject("Scripting.Dictionary")
' Now loop through and name and populate each set with random numbers:
For intSetCounter = 0 to intSetCount
' Make a name for each set:
SetNamesDict( "Set" & intSetCounter) = intSetCounter
' Erase the set for population:
Erase SetArray
Randomize
intSetSize = Int( 20 * Rnd + 1)
Redim SetArray( intSetSize) ' Use this to make random array size also.
For intCount = 0 to intSetSize
Randomize
SetArray( intCount) = Int( 1000 * Rnd + 1)
Next
' Now save this array of numbers:
arrayOfSets( intSetCounter) = SetArray
Next
'Now finish with saving all sets inside there unique file by there
dictionary name.
SaveSets SetNamesDict, arrayOfSets
Sub SaveSets( dictSetNames, arrayForSets)
' Open Files To Write the sets data, using the set name as the file name.
' File open options:
Const ForReading = 1, ForWriting = 2, ForAppending = 8
' File data types:
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
' Init all variables used:
Dim fso, ts, fileObj, FileName, SetName, Count
Set fso = CreateObject("Scripting.FileSystemObject")
For Each SetName In dictSetNames
' Create the file, and obtain a file object for the file.
FileName = SetName & ".txt"
fso.CreateTextFile FileName
Set fileObj = fso.GetFile( FileName)
' Open a text stream for output.
Set TS = fileObj.OpenAsTextStream( ForWriting, TristateUseDefault)
msgBox (SetName & " Array Size Is: " & UBound( arrayForSets(
dictSetNames( SetName))))
For count = 0 To UBound( arrayForSets( dictSetNames( SetName)))
' Write to the text stream.
TS.WriteLine arrayForSets( dictSetNames( SetName))( count)
Next
TS.Close
Next
End Sub