Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default changing the fill color on a cell

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







  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default changing the fill color on a cell

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
,
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
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
Changing the fill color of a cell cquintero57 Excel Discussion (Misc queries) 2 December 11th 07 04:20 PM
Changing Fill color choices Steven Excel Discussion (Misc queries) 4 March 2nd 05 04:05 AM
Changing cell fill color as result of 'if' statement jason Excel Programming 0 August 31st 03 04:51 PM
Changing cell fill color as result of 'if' statement Tom Ogilvy Excel Programming 0 August 31st 03 04:25 PM
Changing cell fill color as result of 'if' statement Phobos Excel Programming 0 August 31st 03 12:41 PM


All times are GMT +1. The time now is 09:33 AM.

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"