+ Reply to Thread
Results 1 to 3 of 3

How to define a function that can sum all cells' value and then return the color of sum?

Hybrid View

  1. #1
    Registered User
    Join Date
    03-21-2013
    Location
    NY
    MS-Off Ver
    Excel 2010
    Posts
    6

    How to define a function that can sum all cells' value and then return the color of sum?

    I want to define a function that can sum all cells' value and then return the color of sum number
    For example, A1:A10 is 1 to 10, the sum of A1:A10 is 55, and at the same time the interior color of another cell would change to the color that the colorindex

     Function iSumColor(r As Range, v As Range) As Double
    Dim q As Range
        Dim result As Double
        result = 0
        For Each q In r
            
            result = result + q.Value
            
        Next
    
         Dim c As Range
         For Each c In q
         c.Interior.ColorIndex = result
    
        Next
    
       iSumColor = result
    
    End Function
    and I input the function "=iSumColor(A1:A10, B1)" in any cell, the cell should be 55, and the B1 cell's color should be color of index is 55, how do I do it?
    what's wrong with my syntax?
    Last edited by bryansky; 03-26-2013 at 01:25 AM.

  2. #2
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: How to define a function that can sum all cells' value and then return the color of su

    Hi

    Firstly

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code in [CODE] [/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    A function should only be used to return a result, not make a change to anything on the sheet. In this case, you are making a change to a cell format, so you should be using a sub.

    Try the code below. When it runs, you will be asked to highligh a range. Select the range A1:A10, and continue.

    Sub aaa()
      Dim Rng As Range
      Set Rng = Application.InputBox("Select range to action", Type:=8)
      
      holder = 0
      For Each ce In Rng
        holder = holder + ce.Value
        ce.Interior.ColorIndex = holder
      Next ce
    End Sub
    rylo

  3. #3
    Registered User
    Join Date
    03-21-2013
    Location
    NY
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: How to define a function that can sum all cells' value and then return the color of su

    Thanks very much

+ 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