Solved

Function to sum a range and set BG color based on result

Posted on 2011-02-15
8
544 Views
Last Modified: 2013-11-25
I cannot get this to work
I have searched for a solution but I have not found one.
Close but not there yet. The statement  rCell.Interior.Color = iCol1 fails. Please help.
The content of the cell: "= SumAndHighlight(D26,D19:D25)"
Also, how can I indicate the cell that contains the formula without explicitly passing it in? i.e. Not have to pass in the first parameter "D26" but have the color in D26 change?
***
Public Function SumAndHighlight(rCell As Range, rSumRange As Range)
  Dim vResult As Integer
  Dim iCol1 As Long
  Dim iCol2 As Long
  Dim iCol3 As Long
  iCol1 = Range("a70").Interior.Color 'Red
  iCol2 = Range("a71").Interior.Color 'Green
  iCol3 = Range("a72").Interior.Color 'Yellow
  vResult = WorksheetFunction.Sum(rSumRange)
  If vResult > 1 Then
    rCell.Interior.Color = iCol1   '<------- This statement fails
  ElseIf vResult < 1 Then
    rCell.Interior.Color = iCol2   '<------- This statement fails
  Else
    rCell.Interior.Color = iCol3   '<------- This statement fails
  End If
  SumAndHighlight = vResult
End Function
0
Comment
Question by:lpollis
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 11

Expert Comment

by:Runrigger
ID: 34897755
Can you tell me the error if you have one?

Can you also show me the calling routine, or attach an example.

  If vResult > 1 Then
    rCell.Interior.Color = iCol1   '<------- I can get this wor work
  ElseIf vResult < 1 Then
    rCell.Interior.Color = iCol2   '<------- I can also get this to work
  Else
    rCell.Interior.Color = iCol3   '<------- This condition will currently not be met because either one of the above will trap first!
  End If
0
 
LVL 24

Accepted Solution

by:
StephenJR earned 250 total points
ID: 34897807
I think a function can only affect the cell in which it is placed, and cannot change formatting.
0
 
LVL 11

Expert Comment

by:Runrigger
ID: 34897855
Ah, that could be why mine works, I converted it to a sub!

Author, what is the reason for you having a function?
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

Author Comment

by:lpollis
ID: 34898200
Thanks for your responses.
I'll take them in order.

Runrigger: The error is "A value use in the formula is of the wrong data type." Also, the third condition is met if the value is equal to 1.

StephenJR: I have seen some examples (I believe) where the color can be changed just by selecting the cell in a function call. Or colors of other cells can be set when a cell has the focus. However, you may be right but how to know for sure. Is there any documentation that you can point me to? I would love to know for sure.

Runrigger:  I can try to convert it to a Sub but I wanted the Function call as it is placed in the cell where I want that action. I may have other cells that I do not want this color change. I guess I can pursue this a little further but I really wanted to use the function call.  
0
 
LVL 11

Expert Comment

by:Runrigger
ID: 34898222
You would never believe that I was a mathematician, LOL!

Busy day at work.
0
 
LVL 24

Expert Comment

by:StephenJR
ID: 34898228
See here: http://support.microsoft.com/kb/170787

Runrigger's approach is one way to go.
0
 
LVL 42

Expert Comment

by:dlmille
ID: 34898873
I believe you can do this with conditional formatting.  I don't think a function (or subroutine) is necessary.

Since you initially were developing a UDF, I assume you want to "point at the cell" and do some operation.  Certainly a macro would work on the active cell.

However, isn't it just as easy (or easier) to setup conditional formatting?

Perhaps it depends on how many of these you're putting out there.  But you can't do =subroutine(,) in a formula, so you have to specify the parameters and run the macro - so this could be less labor intensive.

Anyway, another option.

Dave
ColorCellBasedonSumRange.xlsm
0
 

Author Closing Comment

by:lpollis
ID: 34899076
My thanks to all the responders. I am awarding the solution to Stephen because he identified the problem (You cannot do this in a formula!) and pointed me to the documentation that supported his point.
And I agree with dimille in that this can be done with conditional formatting (at least for this requirement). I was looking beyond the capabilities of conditional formatting and I will have to use subroutines if that becomes necessary.
I was surprised that the error message didn't help more. It would have made things easier.
Thanks to all for your assistance.
Lp
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question