+ Reply to Thread
Results 1 to 11 of 11

Return ColorIndex array

  1. #1
    Forum Expert
    Join Date
    12-24-2004
    Location
    Sweden
    Posts
    1,256

    Return ColorIndex array

    Hello,

    I thougt I would 'try' to learn VBA.

    My first attempt is to return a simple Array of ColorIndexes.
    Could someone please help me with what is wrong.

    Ola


    Please Login or Register  to view this content.

  2. #2
    Forum Expert dominicb's Avatar
    Join Date
    01-25-2005
    Location
    Lancashire, England
    MS-Off Ver
    MS Office 2000, 2003, 2007 & 2016 365
    Posts
    4,867

    Smile

    Good afternoon olasa

    Congrats on taking the plinge!! It seems you're already into the weeping and gnashing of teeth stage . The good news is it doesn't get any better ...

    If you're trying to return a series of numbers you're going to have to return it as a string. That was the problem with your function - it was returning multiple values, but a function can only return one value. I have made an amendment that shows each ColorIndex value seperated by a comma.

    Please Login or Register  to view this content.
    Also, when working with functions and ColorIndex you don't really need to use Application.Volatile, as the ColorIndex property doesn't get updated when a cell is changed - only when you force a recalc.

    HTH

    DominicB
    Please familiarise yourself with the rules before posting. You can find them here.

  3. #3
    Forum Expert
    Join Date
    12-24-2004
    Location
    Sweden
    Posts
    1,256

    UDF that Return an Array


    I had a big laugh I must say. Thanks for the cheers.
    I did not think I could stay anonymous for very long so I just had to bite it and come clean.

    Thanks, but I want a VBA function that returns a VBA Array like the examples below. Not a text string - if possible.
    So when I read "it was returning multiple values" - I sounded as if I was on the right path.

    I also read that: "a function can only return one value" ??
    Am I missing something or...I had a look at this and this - but I didn't know if it could solve my problem.


    And yes I am tossing books and loosing some hair over this.
    //Ola


    Please Login or Register  to view this content.
    I might as well attach my feable attemps
    Attached Files Attached Files

  4. #4
    Forum Expert
    Join Date
    12-24-2004
    Location
    Sweden
    Posts
    1,256

    UDF returns Cell ColorIndex Array

    For the record, solved. Questionably pretty, but a start.
    It returns either a ColorIndex Row Array or Column Array.

    //Ola

    Please Login or Register  to view this content.

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    You could return an array the same size and shape as the range. And if you're learning VBA, a good habit to get into is to declare all variables, which you can enforce by adding Option Explicit at the top of every module. And I wouldn't choose variable names that are the same as VBA key words.
    Please Login or Register  to view this content.
    Last edited by shg; 08-01-2008 at 03:02 PM.

  6. #6
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582
    Can I jump in since I am trying to improve my VBA skills also?

    shg, you said not to use Excel terms as variables but then you did this
    Please Login or Register  to view this content.
    I can't tell where we are using cell as a variable and where it's an excel term in your code.

    Question2: Why didn't you initially dim aCl as (r.row to r.row...) instead of using the redim?

    Thanks in advance.

    ChemistB

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    I can't tell where we are using cell as a variable and where it's an excel term in your code.
    It sounds like you think cell is a keyword; it's not. Cells is a both a property and a collection object. The variable is used here:
    Please Login or Register  to view this content.
    Why didn't you initially dim aCl as (r.row to r.row...) instead of using the redim?
    Because the dimensions aren't known at compile time; you don't know what size to make the array until run-time, so the compiler cannot allocate it statically. It has to be a dynamic array.
    Last edited by shg; 08-01-2008 at 03:40 PM.

  8. #8
    Forum Expert
    Join Date
    12-24-2004
    Location
    Sweden
    Posts
    1,256

    Thanks

    Shg: thanks, much cleaner solution. I enjoy when the solution can delivers more from less.

    Just tell me, why are these 'unfamiliar' Datatypes better than for instance Integer/Range?
    Dim aCI() As XlColorIndex
    Dim cell As Excel.Range

    ChemistB: thanks for questioning. I was surprised I knew the answer to Question 2.

    Ola

  9. #9
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    You could use a Long, but the ColorIndex property is a Variant. And using the enumeration allows Intellisense to work: If you type

    Please Login or Register  to view this content.
    ... you'll see the enumerations as soon as you type the = sign.

    As to dimensioning variables to include their library names (e.g., Excel.Range instead of just Range), it's a habit I'm trying to get into. Word, for example, also has a Range object, and if you were automating Word from Excel it would be important to distinguish.

    But I wasn't consistent, because I didn't do that in the function declaration.

    IMO, that can go too far. I see people that always use

    Please Login or Register  to view this content.
    ... while I just use
    Please Login or Register  to view this content.
    ... because brevity has its own merits, and I find verbose code hard to read.

    However, you can also use
    Please Login or Register  to view this content.
    ... but I avoid that, because Intellisense works with the first two ways, but not with the third.

    I also mostly avoid eliding default properties, and so use

    Please Login or Register  to view this content.
    ... but the subscripts are themselves arguments to another default property:

    Please Login or Register  to view this content.
    and that, I think, is a bridge too far.

    Since I am the only person that will read most code I write, I focus on making it understandable after years and many good, smokey, Scotches between visits.
    Last edited by shg; 08-01-2008 at 07:13 PM.

  10. #10
    Forum Expert
    Join Date
    12-24-2004
    Location
    Sweden
    Posts
    1,256

    ...

    Shg: thanks for the dialectal sides of the language.
    It will probably take me some time to build up rules for a good recollection language. Associations are quite individual.

    Please Login or Register  to view this content.
    Sound like a good rule

    Please Login or Register  to view this content.
    So since it's defined and makes it easier to read. Why not use it (once you know it exists)

    Please Login or Register  to view this content.
    I'm not there yet; programming across application borders. But important when new Applications come into the game.

    Please Login or Register  to view this content.
    I agree, it makes it more readable.

    I think the best judge of a good code is if you can read it
    after you have forgotten you ever wrote it. Due to a good Scotsh or not...
    //Ola

  11. #11
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    Quote Originally Posted by shg
    ColorIndex property is a Variant
    Quote Originally Posted by olasa
    So since it's defined and makes it easier to read. Why not use it (once you know it exists)
    It's a Variant is so that the property can return, for example, Null if you ask for the color index of a range that does not have a common value. Since each variable of my array is intended to receive the color index of a single cell, they will always be Longs (which is what enumerations are).
    I'm not there yet; programming across application borders. But important when new Applications come into the game.
    Me either, actually. I'm just trying to get a better understanding of the language.

    Here's another example from a thread yesterday. The OP asked:

    Please Login or Register  to view this content.
    Is there a way to change the picture formula without selecting the picture?
    You can set a ton of properties on Selection, without knowing what aspect of the Selection you're operating on, and neither the macro recorder nor Help makes it easy to figure out.

    The answer, it turns out, is
    Please Login or Register  to view this content.
    Try to find the Pictures collection or the Picture object in Help, or where the Formula property of a Picture is explained.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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