Solved

Pass Array Record

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

777 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