Consulting

Results 1 to 14 of 14

Thread: CellColor Macro Not Working

  1. #1
    VBAX Regular
    Joined
    Jul 2007
    Posts
    20
    Location

    CellColor Macro Not Working

    Hello All,

    I cannot figure out what I am doing wrong. Any help would be appreciated.

    Function cellcolor(r As Range)
    Application.Volatile
    cellcolor = r.Interior.ColorIndex
    End Function
    Function FontColor(r As Range)
    Application.Volatile
    FontColor = r.Font.ColorIndex
    End Function


  2. #2
    VBAX Regular
    Joined
    Jul 2007
    Posts
    71
    Location
    What are you trying to do with this function?

  3. #3
    Functions are not allowed to change anything, the only thing they can do is return a value to the calling cell
    2+2=9 ... (My Arithmetic Is Mental)

  4. #4
    VBAX Regular
    Joined
    Jul 2007
    Posts
    20
    Location
    I am trying to create a function that will give me a numerical value based on the color of a cell

  5. #5
    VBAX Tutor david000's Avatar
    Joined
    Mar 2007
    Location
    Chicago
    Posts
    276
    Location

    One does Font the other does Interior

    [vba]
    Function SumColorF(Area As Range, ci As Integer)
    Dim sng As Single
    Dim rng As Range

    For Each rng In Area
    If rng.Font.ColorIndex = ci Then
    sng = sng + rng.Value
    End If
    Next rng
    SumColorF = sng
    End Function
    [/vba] [vba]
    Function SumColor(Area As Range, ci As Integer)
    Dim sng As Single
    Dim rng As Range

    For Each rng In Area
    If rng.Interior.ColorIndex = ci Then
    sng = sng + rng.Value
    End If
    Next rng
    SumColor = sng
    End Function

    [/vba]

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    It does what you describe, so what is the problem?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    VBAX Regular
    Joined
    Jul 2007
    Posts
    20
    Location
    when I load and use the function in my company's Excel book, I get an error in the cell.

  8. #8
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Can you post a book to demonstrate? Use Manage attachments in the Go Advanced section.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    If you get a #NAME error, have you loaded the functions in a standard code module.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  10. #10
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    Did you put the code in the workbook you are using it in? If not, you will need to prefix the function with the name of the workbook where the code is - e.g.:
    =Personal.xls!cellcolor(A1)
    Also, do you have macros enabled?

    Regards,
    Rory

  11. #11
    VBAX Regular
    Joined
    Jul 2007
    Posts
    20
    Location

    I attached the file

    Please let me know what I am doing wrong. The security settings are on low and the script is in the correct sheet.


  12. #12
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    The macros need to be in a standard module as xld said, not in the worksheet code module. You are also using the SumColor function incorrectly. See attached (if I get this right!)

  13. #13
    VBAX Regular
    Joined
    Jul 2007
    Posts
    20
    Location
    Thanks Rory, Xld and the rest of you guys. I think I will pick up a VBA book to familiarize myself. Is there one that you would recommend for a beginner?

  14. #14
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    Depends how quickly you pick stuff up really. A Dummies book might get too basic too quickly; John Walkenbach's Power Programming books are good, as are the Wrox Press "Excel 200x VBA" ones. Then once you are through those, the Professional Excel Development book by Bovey, Bullen and Green is great!

Posting Permissions

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