PC Review


Reply
Thread Tools Rate Thread

changing the fill color on a cell

 
 
jseger22@yahoo.com
Guest
Posts: n/a
 
      11th Jan 2008
Hi,

I am trying to fill the color of a cell based on if statements, but I
don't know how to reference the cell. Everything works except when I
try to reference the CellValue to fill the cell and I am not sure what
I am doing wrong because I am only a novice at VBA. My code looks
like

Sub Macro2()
Dim lastyear As Double
Dim target As Double
Dim CellValue As Double
Dim i As Integer

For i = 5 To 15

CellValue = Cells(i, 37)
target = Cells(i, 3) * 100
lastyear = Cells(i, 25)


If CellValue >= lastyear And CellValue >= target Then
CellValue.Interior.ColorIndex = 4
ElseIf CellValue >= lastyear And CellValue < target Then
CellValue.Interior.ColorIndex = 3
ElseIf CellValue < lastyear And CellValue < target Then
CellValue.Interior.ColorIndex = 6
Else
CellValue.Interior.ColorIndex = 3
End If
Next
End Sub







 
Reply With Quote
 
 
 
 
JE McGimpsey
Guest
Posts: n/a
 
      11th Jan 2008
CellValue is declared as type Double, but you're trying to use it as a
range object.

One possibility:

Dim lastyear As Double
Dim target As Double
Dim CellValue As Double
Dim i As Integer

For i = 5 To 15

CellValue = Cells(i, 37).Value
target = Cells(i, 3).Value * 100
lastyear = Cells(i, 25).Value


If CellValue >= lastyear And CellValue >= target Then
Cells(i, 37).Interior.ColorIndex = 4
ElseIf CellValue >= lastyear And CellValue < target Then
Cells(i, 37).Interior.ColorIndex = 3
ElseIf CellValue < lastyear And CellValue < target Then
Cells(i, 37).Interior.ColorIndex = 6
Else
Cells(i, 37).Interior.ColorIndex = 3
End If
Next i


A somewhat more efficient way would be:

Dim lastyear As Double
Dim target As Double
Dim i As Integer
Dim nCI As Long

For i = 5 To 15
target = Cells(i, 3).Value * 100
lastyear = Cells(i, 25).Value

With Cells(i, 37)
nCI = 3
If .Value >= lastyear Then
nCI = nCI - (.Value >= target)
Else
nCI = nCI - 2 * (.Value < target)
End If
.Interior.ColorIndex = nCI
End With
Next i



In article
<1a496a49-df04-458b-a1cc-(E-Mail Removed)>,
(E-Mail Removed) wrote:

> Hi,
>
> I am trying to fill the color of a cell based on if statements, but I
> don't know how to reference the cell. Everything works except when I
> try to reference the CellValue to fill the cell and I am not sure what
> I am doing wrong because I am only a novice at VBA. My code looks
> like
>
> Sub Macro2()
> Dim lastyear As Double
> Dim target As Double
> Dim CellValue As Double
> Dim i As Integer
>
> For i = 5 To 15
>
> CellValue = Cells(i, 37)
> target = Cells(i, 3) * 100
> lastyear = Cells(i, 25)
>
>
> If CellValue >= lastyear And CellValue >= target Then
> CellValue.Interior.ColorIndex = 4
> ElseIf CellValue >= lastyear And CellValue < target Then
> CellValue.Interior.ColorIndex = 3
> ElseIf CellValue < lastyear And CellValue < target Then
> CellValue.Interior.ColorIndex = 6
> Else
> CellValue.Interior.ColorIndex = 3
> End If
> Next
> End Sub

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing the fill color of a cell cquintero57 Microsoft Excel Misc 2 11th Dec 2007 04:20 PM
Re: Changing cell fill color as result of 'if' statement jason Microsoft Excel Programming 0 31st Aug 2003 04:51 PM
Re: Changing cell fill color as result of 'if' statement Tom Ogilvy Microsoft Excel Programming 0 31st Aug 2003 04:25 PM
Re: Changing the default cell fill color Coopfab Microsoft Excel Misc 1 28th Aug 2003 02:10 PM
Re: Changing the default cell fill color Coopfab Microsoft Excel Misc 0 28th Aug 2003 12:58 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:28 PM.