+ Reply to Thread
Results 1 to 3 of 3

Excel VBA - set Class properties from within the class

  1. #1
    Forum Contributor
    Join Date
    05-26-2004
    Location
    Halifax, UK
    MS-Off Ver
    Office 2016
    Posts
    260

    Excel VBA - set Class properties from within the class

    I have a spreadsheet which stores client details in a typical columns/rows fashion, i.e. Col A = client ID, Col B = client name, Col C = client type

    The worksheet with this data on is accessed by various VBA modules, however the current way of referring to a specific field is somewhat inefficient. The code modules process the client at the top of the list (always row 2), then delete this row and process the next client etc. This part is okay but there is a code module that looks at the Client Type (Col C) field, which is always cell C2. In order to refer to this cell the code currently starts:

    Please Login or Register  to view this content.
    What I really want to be able to do is read in all the data for the client in row 2 into an object, or perhaps a custom data type? I want the VBA code to read more like:

    Please Login or Register  to view this content.
    I did think that creating an object might be the way to do it, however my experience of creating objects is fairly limited. I know you can create properties using the Let/Get keywords, but I am unsure how I would 'read in' the data for the next Client. I think I would want a method along the lines of:

    ClientObject.GetNextClient

    Which then reads in the data for client ID, client name, client type etc. However I want to be able to read these properties back using ClientObject.ClientID, ClientObject.ClientName etc

    Is this possible? I guess I'm asking how do I pass values to multiple object properties in one go? Is it possible to do this by running a single procedure which then passes values to a range of properties?

    Thanks in advance
    -Rob

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,436

    Re: Excel VBA - set Class properties from within the class

    You don't have to use a class if you don't want.
    This uses a Type and a enum list.

    Please Login or Register  to view this content.
    Regardless of approach you need a function to make reading of the data per client easier.
    Cheers
    Andy
    www.andypope.info

  3. #3
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Excel VBA - set Class properties from within the class

    I would use the approach of setting pointers rather than storing values.

    I would create a class with a ClientRow property (a range object). Then ClientName, ClientID, ClientType would be derived from that value.
    The NextClient (and my added GetClientFromName and AddNewClient) sub would change ClientRow. Since ClientName etc are derived, they would follow along.

    (In my example, row 1 of ClientData sheet is headers, not data.)

    Please Login or Register  to view this content.
    Last edited by mikerickson; 02-14-2015 at 09:46 AM.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] How to set class object properties
    By Tsjallie in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 07-18-2014, 02:58 PM
  2. Properties in a class - can't write to them
    By Lazhal in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-03-2010, 04:16 PM
  3. Problems Setting properties of class module
    By Andibevan in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-02-2005, 09:05 AM
  4. Multidimensional Arrays as Properties in Class Files?
    By Trip in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-26-2005, 02:05 PM
  5. Custom Class and Properties
    By Rich_z in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-12-2005, 11:57 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1