+ Reply to Thread
Results 1 to 11 of 11

Return ColorIndex array

Hybrid View

  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


    Function RangeColor(Range As Range)
    
    Application.Volatile True
    Dim cell As Range
    
    For Each cell In Range
        RangeColor = cell.Interior.colorindex
    Next
    
    End Function

  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.

    Function RangeColor(Range As Range)
    Application.Volatile True
    Dim cell As Range
    a = ""
    For Each cell In Range
    a = a & cell.Interior.ColorIndex & ", "
    '    RangeColor = cell.Interior.ColorIndex
    Next
    RangeColor = Left(a, Len(a) - 2)
    End Function
    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


    Function MonthNames()
        MonthNames = Array("Jan", "Feb", "Mars")
    End Function
    -------
    
    Private Function ReturnArray() As Variant
    
    Dim x(3) As Integer 'Declare a Static Integer Array of 4 elements
    x(0) = 1
    x(1) = 2
    x(2) = 3
    x(3) = 4
    ReturnArray = x 'Pass the array back as a return value
    
    End Function
    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

    Function CellColor(Range As Range)
    
    Dim i As Integer
    j = Application.Max(Range.Rows.Count, Range.Columns.Count)
    ReDim Cell(1 To j)
    
    Application.Volatile True
    
    For i = 1 To j
        Cell(i) = Range(i).Interior.colorindex
    Next
    
    RangeColor = Application.Transpose(Cell)
    
    End Function

  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.
    Function CellColor(r As Range) As XlColorIndex()
        Dim aCI()   As XlColorIndex
        Dim cell    As Excel.Range
    
        ReDim aCI(r.Row To r.Row + r.Rows.Count - 1, r.Column To r.Column + r.Columns.Count - 1)
    
        For Each cell In r
            aCI(cell.Row, cell.Column) = cell.Interior.ColorIndex
        Next
    
        CellColor = aCI
    End Function
    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
        Dim cell    As Excel.Range
    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

+ 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