Solved

Pass Array Record

Posted on 2014-03-28
4
221 Views
Last Modified: 2014-03-31
Hello,

Question Setup

I have a VBA class I created for an Excel project I'm working on.

I have a 2 dimensional array of values that have been read from a worksheet.

For each record in that array, I want to create an instance of the class and then initialize the variables in that class with the values in the array.

Question

What I want to know is ... is there a way in VBA to just pass that array record to the initializing subroutine in the class? Sure, I could just pass them all with array(x,y) notation but I'm wondering if there is a more elegant way. I want to pass just that one record and all it's values (7 currently), not the whole array.
0
Comment
Question by:qholmberg
4 Comments
 
LVL 45

Expert Comment

by:aikimark
ID: 39963771
VBA classes do not allow you to parameterize the class_initialize() routine.  You could create your own method in the class that could assign the values.

This seems to fit the 'factory pattern'.  What you should consider is a non-class function that is passed parameters and both instantiates the class object and assigns property values.  This function would return a properly setup class object.  I would assume that you would iterate your array, passing these values to such a function.
0
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 500 total points
ID: 39966272
You can't, as has been mentioned, use the class_initialize event, but you can certainly use a routine in the class and pass an array to it using Application.Index. Here's a very simplistic example:
Class1:
Option Explicit
Public item1
Public item2
Public item3
Public item4
Public item5
Public item6
Public item7
Sub LoadData(vin)
   item1 = vin(1)
   item2 = vin(2)
   item3 = vin(3)
   item4 = vin(4)
   item5 = vin(5)
   item6 = vin(6)
   item7 = vin(7)
End Sub

Open in new window


Calling module:
Option Explicit
Dim someCollection                 As Collection
Sub Foo()
   Dim vData
   Dim n
   Dim lastRow                     As Long
   Dim myClass                     As Class1

   Set someCollection = New Collection
   lastRow = Cells(Rows.count, "A").End(xlUp).Row

   vData = Range("A2:G" & lastRow).Value
   For n = LBound(vData, 1) To UBound(vData, 1)
      Set myClass = New Class1
     ' pass the current "row" of the array
      myClass.LoadData Application.Index(vData, n, 0)
      someCollection.Add myClass
   Next n
Debug.Print someCollection(1).item1
End Sub

Open in new window

0
 

Author Closing Comment

by:qholmberg
ID: 39966424
That is precisely what I was looking for. Thanks!

I only wish Microsoft had made a more readable way to go about it but, it will still make my code a little easier to maintain over time.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

863 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

25 Experts available now in Live!

Get 1:1 Help Now