Solved

Pass Array Record

Posted on 2014-03-28
4
224 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa‚Ķ

724 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