PC Review


Reply
Thread Tools Rate Thread

Change background color based on cell value in vb?? Current code notworking...

 
 
Naji
Guest
Posts: n/a
 
      11th Feb 2008
Hello,

I'm having a hard time with this. I need to change the background
color for a cell if the number in B3 is equal to "1", but this code is
not working. The B3 value is based on a vlookup formula. This is what
I have:

Sub testme()
Dim myRng As Range
Dim myRow As Range
Dim myEmptyCells As Range

With Worksheets("PO Performance")
Set myRng = .Range("F3:X3")
End With

Set myEmptyCells = Nothing
On Error Resume Next
Set myEmptyCells = myRng.Cells.SpecialCells(xlCellTypeBlanks)
On Error GoTo 0


If myEmptyCells Is Nothing Then
MsgBox "no empties!"
Exit Sub
End If


For Each myRow In myRng.Rows
Set myEmptyCells = Nothing
On Error Resume Next
Set myEmptyCells = myRow.Cells.SpecialCells(xlCellTypeBlanks)
On Error GoTo 0
If myEmptyCells Is Nothing Then
'keep looking
Else
********************This is the part in question below.
The value in B3 is equal to one but *****************************this
code does not acknowledge that fact. What am I doing wrong?


myEmptyCells.Cells(1).Formula = "XXXXX"
If Range("B3").Value = 1 Then
myEmptyCells.Cells(1).Interior.ColorIndex = 3
End If
'stop looking
Exit For
End If
Next myRow

End Sub
 
Reply With Quote
 
 
 
 
Bernie Deitrick
Guest
Posts: n/a
 
      11th Feb 2008
Naji,

Be more specific about the location of cell B3:

If Range("B3").Value = 1 Then

Should be

If Worksheets("Sheetname").Range("B3").Value = 1 Then

You may also need to roung the value to 0 decimal places before doing the
comparison with 1, if the number being returned isn't truly 1 but just looks
that way (0.99999999999999997)

HTH,
Bernie
MS Excel MVP

"Naji" <(E-Mail Removed)> wrote in message
news:8c90d81a-9aa8-4fbe-9588-(E-Mail Removed)...
> Hello,
>
> I'm having a hard time with this. I need to change the background
> color for a cell if the number in B3 is equal to "1", but this code is
> not working. The B3 value is based on a vlookup formula. This is what
> I have:
>
> Sub testme()
> Dim myRng As Range
> Dim myRow As Range
> Dim myEmptyCells As Range
>
> With Worksheets("PO Performance")
> Set myRng = .Range("F3:X3")
> End With
>
> Set myEmptyCells = Nothing
> On Error Resume Next
> Set myEmptyCells = myRng.Cells.SpecialCells(xlCellTypeBlanks)
> On Error GoTo 0
>
>
> If myEmptyCells Is Nothing Then
> MsgBox "no empties!"
> Exit Sub
> End If
>
>
> For Each myRow In myRng.Rows
> Set myEmptyCells = Nothing
> On Error Resume Next
> Set myEmptyCells = myRow.Cells.SpecialCells(xlCellTypeBlanks)
> On Error GoTo 0
> If myEmptyCells Is Nothing Then
> 'keep looking
> Else
> ********************This is the part in question below.
> The value in B3 is equal to one but *****************************this
> code does not acknowledge that fact. What am I doing wrong?
>
>
> myEmptyCells.Cells(1).Formula = "XXXXX"
> If Range("B3").Value = 1 Then
> myEmptyCells.Cells(1).Interior.ColorIndex = 3
> End If
> 'stop looking
> Exit For
> End If
> Next myRow
>
> 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
Change background color of cell based on vlookup in cell Antney Microsoft Excel Misc 1 19th Oct 2009 10:55 PM
change the color of cell background based on a result Rich Microsoft Excel Worksheet Functions 2 27th Jan 2009 07:17 PM
Change tab color based on current color of a cell MarkT Microsoft Excel Misc 0 22nd May 2008 05:46 PM
change the background's color for current cell moshe_xls Microsoft Excel Programming 4 15th Apr 2008 07:36 PM
Cell compare and based on the value change the cell background color pftiv Microsoft Excel Worksheet Functions 2 15th Jan 2004 04:24 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:11 AM.