Solved

Pass Array Record

Posted on 2014-03-28
4
219 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
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.

759 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

20 Experts available now in Live!

Get 1:1 Help Now