Solved

Pass Array Record

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
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.

726 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