Solved

Creating arrays using dynamic array names

Posted on 2011-02-21
6
239 Views
Last Modified: 2012-05-11
Hi,

I am trying to create a function that will create several arrays all from different files. I want to be able to pass the array name and file name to the function and have it create the arrayname based on what I have sent.

Here is a simplified sample

[vba]Sub GetArrays
MyArrayName = "Commision"
MyFileName = "c:\commisson.xls"
Call MakeArray(MyArrayName, MyFileName)
end sub

Function MakeArray
'open file,
' read data...
MyArrayName(i, j) = Range(data).Value
end function[/vba]

 I have several files to import. Is this possible? and how do I go about doing this?
0
Comment
Question by:kuda27
  • 3
  • 2
6 Comments
 
LVL 18

Expert Comment

by:Cluskitt
Comment Utility
MyArrayName(i, j) = Range(data).Value
Can't do this. What you can do is:
MyArrayName = Range(data)

This will create a 2 dimension array (for rows and columns). You can always check the size with Ubound.

Also: MyArrayName = "Commision"
This will effectively set MyArrayName as a single variable of String type. Either don't declare it, or declare it as variant (which it needs to be to get a range assigned to it anyway).
0
 

Author Comment

by:kuda27
Comment Utility

Thanks for the response Cluskitt.

Still wondering if it is possible to have the MAkeArray function, where the Array name is a dynamic. That is my key question.  
0
 
LVL 18

Expert Comment

by:Cluskitt
Comment Utility
What you want is something like:

Sub GetArrays
Dim MyArrayName1, MyArrayName2 As Variant
MyFileName = "c:\commisson.xls"
MyArrayName1 = MakeArray(MyFileName)
MyFileName = "c:\commisson2.xls"
MyArrayName2 = MakeArray(MyFileName)
end sub

Function MakeArray(nameVar As String) As Range
'open file,
' read data...
Return Range(data)
End Function


Basically, the function will take 1 argument, a string, that contains the filename. It will then perform whatever operations you want, and return the data range. It is then assigned to whatever array you want.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:kuda27
Comment Utility
Thanks again, that makes sense. The problem is that I don't know how many tables will be imported at any one time. Is it possible to then have this so that imports all the  tables  without having to adjust the code each time.
0
 
LVL 18

Accepted Solution

by:
Cluskitt earned 500 total points
Comment Utility
That would depend on how you're loading them. But what you basically need is to build a For, While or Repeat loop. If you want each table loaded in a different array, you can just create an array of arrays. Something like:

Dim MyArrayName(1 To 2) As Variant
ReDim MyArrayName(1 To TablesCount) 'TablesCount is a variable with the number of tables
For x=1 To TablesCount
  'Read whatever filename you want
  MyArrayName(x) = MakeArray(MyFileName)
Next


You can then access each like this:

MyArrayName (5) (3,2)  'this will get the the 3rd line and 2nd column in the 5th array/table
 
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
You can use a COLLECTION where you add the Arrays to the Collection using the Name as the Key:
http://msdn.microsoft.com/en-us/library/aa242681(VS.60).aspx
http://msdn.microsoft.com/en-us/library/aa265006(VS.60).aspx

So you'd declare just ONE Collection to hold the dynamic number of arrays.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

A short article about a problem I had getting the GPS LocationListener working.
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now