Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default Using VBA to change the color of the active cell if the value in thatcell is less than 15%

Dear All

I am trying to simply create a VBA script to change the color of the
active cell if the resulting value in that cell is less than 15%. In
that cell there is a simple division formula referring to two other
cells to give a percentage in that cell. I have come to the conclusion
that a Function is better than a Sub but the following VBA script
gives me a 'Value!' error. Any ideas for a rookie, please?

Function EfficiencyRatio()
'
' EfficiencyRatio Macro
' Highlights the cell green if the expense ratio is less than 15%
'
' Keyboard Shortcut: Ctrl+Shift+X
'

Selection.FormatConditions.Add Type:=xlCellValue,
Operator:=xlLess, _
Formula1:="=0.15"

Selection.FormatConditions(Selection.FormatConditi ons.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = -16752384
.TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 13561798
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
End Function
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 363
Default Using VBA to change the color of the active cell if the value in that cell is less than 15%

Use Conditional Formatting under the Format Tab.
Easier...


Corey....
"Pelham" wrote in message
...
Dear All

I am trying to simply create a VBA script to change the color of the
active cell if the resulting value in that cell is less than 15%. In
that cell there is a simple division formula referring to two other
cells to give a percentage in that cell. I have come to the conclusion
that a Function is better than a Sub but the following VBA script
gives me a 'Value!' error. Any ideas for a rookie, please?

Function EfficiencyRatio()
'
' EfficiencyRatio Macro
' Highlights the cell green if the expense ratio is less than 15%
'
' Keyboard Shortcut: Ctrl+Shift+X
'

Selection.FormatConditions.Add Type:=xlCellValue,
Operator:=xlLess, _
Formula1:="=0.15"

Selection.FormatConditions(Selection.FormatConditi ons.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = -16752384
.TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 13561798
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
End Function


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 120
Default Using VBA to change the color of the active cell if the value in that cell is less than 15%

See if this fits...


Sub test()
EfficiencyRatio
End Sub

Function EfficiencyRatio()
If activecell.Value < 0.15 Then
activecell.Interior.Color = 13561798
activecell.Font.Color = -16752384
End If
End Function


Mark Ivey




"Pelham" wrote in message
...
Dear All

I am trying to simply create a VBA script to change the color of the
active cell if the resulting value in that cell is less than 15%. In
that cell there is a simple division formula referring to two other
cells to give a percentage in that cell. I have come to the conclusion
that a Function is better than a Sub but the following VBA script
gives me a 'Value!' error. Any ideas for a rookie, please?

Function EfficiencyRatio()
'
' EfficiencyRatio Macro
' Highlights the cell green if the expense ratio is less than 15%
'
' Keyboard Shortcut: Ctrl+Shift+X
'

Selection.FormatConditions.Add Type:=xlCellValue,
Operator:=xlLess, _
Formula1:="=0.15"

Selection.FormatConditions(Selection.FormatConditi ons.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = -16752384
.TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 13561798
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
End Function


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default Using VBA to change the color of the active cell if the value inthat cell is less than 15%

On Feb 7, 11:06*am, "Mark Ivey" wrote:
See if this fits...

Sub test()
* * EfficiencyRatio
End Sub

Function EfficiencyRatio()
* * If activecell.Value < 0.15 Then
* * * * activecell.Interior.Color = 13561798
* * * * activecell.Font.Color = -16752384
* * End If
End Function

Mark Ivey

"Pelham" wrote in message

...



Dear All


I am trying to simply create a VBA script to change the color of the
active cell if the resulting value in that cell is less than 15%. In
that cell there is a simple division formula referring to two other
cells to give a percentage in that cell. I have come to the conclusion
that a Function is better than a Sub but the following VBA script
gives me a 'Value!' error. Any ideas for a rookie, please?


Function EfficiencyRatio()
'
' EfficiencyRatio Macro
' Highlights the cell green if the expense ratio is less than 15%
'
' Keyboard Shortcut: Ctrl+Shift+X
'


* *Selection.FormatConditions.Add Type:=xlCellValue,
Operator:=xlLess, _
* * * *Formula1:="=0.15"


Selection.FormatConditions(Selection.FormatConditi ons.Count).SetFirstPriori*ty
* *With Selection.FormatConditions(1).Font
* * * *.Color = -16752384
* * * *.TintAndShade = 0
* *End With
* *With Selection.FormatConditions(1).Interior
* * * *.PatternColorIndex = xlAutomatic
* * * *.Color = 13561798
* * * *.TintAndShade = 0
* *End With
* *Selection.FormatConditions(1).StopIfTrue = False
End Function- Hide quoted text -


- Show quoted text -


Thank you Corey and Mark!

Yes, I am aware that the conditional formatting is easier but I am
curious to know how I can create my own VBA to do this so that it runs
automatically whenver I open up a new spreadsheet - instead of having
to set the condition each time...

Mark's suggestion gives me an error message in a cell containing the
formula =1-(B5/B4) which I am trying to change to =EfficiencyRatio(1-
(B5/B4)) so that the color changes if it is less than 15%.
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to i change the active cell highlight color? jkk771 Excel Discussion (Misc queries) 1 February 13th 09 04:31 PM
when I activate a cell i change color of active row & header from HITMAN2 Excel Worksheet Functions 2 April 12th 08 06:33 PM
An auto macro to change color of the active cell Weaver Dean[_2_] Excel Programming 1 December 3rd 07 11:41 PM
Change color on active cell Helen Excel Programming 1 November 16th 05 04:07 AM
How do I change color of active cell in Excel lfletcher Excel Discussion (Misc queries) 4 April 4th 05 06:29 PM


All times are GMT +1. The time now is 02:58 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"