PC Review


Reply
Thread Tools Rate Thread

Conditional Formatting of Cell

 
 
Dee
Guest
Posts: n/a
 
      5th Oct 2008
Hello, I have a worksheet whos data comes from SQL Server db. Autofilter is
turned on. Within the worksheet are many columns. I would like to change
the backcolor of the cells in column C based on the content of the
cell in column D (could be "PASS", "WATCH", or "FAIL") on the same row
(i.e. c1=5 d1='Pass' change backcolor of c1
to Green, or c1=7 d1='WATCH' change backcolor to Yellow, or c1=10 d1='FAIL'
change backcolor to Red). How do I identify the range and apply correct
backcolor to cells within range? Need to be coded using vba macro? --
DC

--
Dee
 
Reply With Quote
 
 
 
 
Gary Keramidas
Guest
Posts: n/a
 
      6th Oct 2008
one other thing, just in case there may not be a value in column D:

Select Case UCase(.Value)
Case "FAIL"
.Offset(, -1).Interior.ColorIndex = 3
Case "WATCH"
.Offset(, -1).Interior.ColorIndex = 19
Case "PASS"
.Offset(, -1).Interior.ColorIndex = 35
Case Else
.Offset(, -1).Interior.ColorIndex = 0
End Select

--


Gary

"Gary Keramidas" <GKeramidasAtMsn.com> wrote in message
news:(E-Mail Removed)...
>i don't always use conditional formatting unless i'm going to change data
>between runs.
>
> so, if you run your query and run code, something like this should work.
>
> Sub test()
> Dim i As Long
> Dim ws As Worksheet
> Dim lastrow As Long
>
> Set ws = Worksheets("Sheet1")
> lastrow = ws.Cells(Rows.Count, "C").End(xlUp).Row
>
> For i = 1 To lastrow
> With ws.Range("D" & i)
> Select Case UCase(.Value)
> Case "FAIL"
> .Offset(, -1).Interior.ColorIndex = 3
> Case "WATCH"
> .Offset(, -1).Interior.ColorIndex = 19
> Case "PASS"
> .Offset(, -1).Interior.ColorIndex = 35
> End Select
> End With
> Next
>
> End Sub
>
> --
>
>
> Gary
>
> "Dee" <(E-Mail Removed)> wrote in message
> news:F8221571-AA91-4CEE-A04D-(E-Mail Removed)...
>> Hello, I have a worksheet whos data comes from SQL Server db. Autofilter
>> is
>> turned on. Within the worksheet are many columns. I would like to
>> change
>> the backcolor of the cells in column C based on the content of the
>> cell in column D (could be "PASS", "WATCH", or "FAIL") on the same row
>> (i.e. c1=5 d1='Pass' change backcolor of c1
>> to Green, or c1=7 d1='WATCH' change backcolor to Yellow, or c1=10
>> d1='FAIL'
>> change backcolor to Red). How do I identify the range and apply correct
>> backcolor to cells within range? Need to be coded using vba macro? --
>> DC
>>
>> --
>> Dee

>
>



 
Reply With Quote
 
Barb Reinhardt
Guest
Posts: n/a
 
      6th Oct 2008
Try this. It will set the conditional format of the cells in the range based
upon the value in the cell to the RIGHT.

Dim myRange As Range
Set myRange = Range("C1:C10")
With myRange
.FormatConditions.Delete
.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=RC[1]=""PASS"""
.FormatConditions(1).Interior.ColorIndex = 4
.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=RC[1]=""WATCH"""
.FormatConditions(2).Interior.ColorIndex = 6
.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=RC[1]=""FAIL"""
.FormatConditions(3).Font.ColorIndex = 2
.FormatConditions(3).Interior.ColorIndex = 3
End With

--
HTH,
Barb Reinhardt

If this post was helpful to you, please click YES below.



"Dee" wrote:

> Hello, I have a worksheet whos data comes from SQL Server db. Autofilter is
> turned on. Within the worksheet are many columns. I would like to change
> the backcolor of the cells in column C based on the content of the
> cell in column D (could be "PASS", "WATCH", or "FAIL") on the same row
> (i.e. c1=5 d1='Pass' change backcolor of c1
> to Green, or c1=7 d1='WATCH' change backcolor to Yellow, or c1=10 d1='FAIL'
> change backcolor to Red). How do I identify the range and apply correct
> backcolor to cells within range? Need to be coded using vba macro? --
> DC
>
> --
> Dee

 
Reply With Quote
 
Dee
Guest
Posts: n/a
 
      6th Oct 2008
Barb, is there a way to code the range for the statement "Set myRange =
Range("C1:C10") " my db will update each month and add rows. Dont want to
have to change code every month.

Thanks,
DC

--
DC


"Barb Reinhardt" wrote:

> Try this. It will set the conditional format of the cells in the range based
> upon the value in the cell to the RIGHT.
>
> Dim myRange As Range
> Set myRange = Range("C1:C10")
> With myRange
> .FormatConditions.Delete
> .FormatConditions.Add Type:=xlExpression, Formula1:= _
> "=RC[1]=""PASS"""
> .FormatConditions(1).Interior.ColorIndex = 4
> .FormatConditions.Add Type:=xlExpression, Formula1:= _
> "=RC[1]=""WATCH"""
> .FormatConditions(2).Interior.ColorIndex = 6
> .FormatConditions.Add Type:=xlExpression, Formula1:= _
> "=RC[1]=""FAIL"""
> .FormatConditions(3).Font.ColorIndex = 2
> .FormatConditions(3).Interior.ColorIndex = 3
> End With
>
> --
> HTH,
> Barb Reinhardt
>
> If this post was helpful to you, please click YES below.
>
>
>
> "Dee" wrote:
>
> > Hello, I have a worksheet whos data comes from SQL Server db. Autofilter is
> > turned on. Within the worksheet are many columns. I would like to change
> > the backcolor of the cells in column C based on the content of the
> > cell in column D (could be "PASS", "WATCH", or "FAIL") on the same row
> > (i.e. c1=5 d1='Pass' change backcolor of c1
> > to Green, or c1=7 d1='WATCH' change backcolor to Yellow, or c1=10 d1='FAIL'
> > change backcolor to Red). How do I identify the range and apply correct
> > backcolor to cells within range? Need to be coded using vba macro? --
> > DC
> >
> > --
> > Dee

 
Reply With Quote
 
FSt1
Guest
Posts: n/a
 
      6th Oct 2008
hi
you will have to have some way of telling vb what range to apply the cf to.
you could add an input box so that the code will ask you what range.
Dim myRange As Range
Dim r As String
r = InputBox("enter range")
Set myRange = Range(r) 'Range("C1:C10")
With myRange
.ect

or if you don't want to be bothered by input, you could do this.

Dim myRange As Range
Dim r As Long
r = Cells(Rows.Count, "C").End(xlUp).Row
Set myRange = Range("C1:C" & r)
with myrange
.ect
this way vb would just find the last row and use it in the range.

regards
FSt1

"Dee" wrote:

> Barb, is there a way to code the range for the statement "Set myRange =
> Range("C1:C10") " my db will update each month and add rows. Dont want to
> have to change code every month.
>
> Thanks,
> DC
>
> --
> DC
>
>
> "Barb Reinhardt" wrote:
>
> > Try this. It will set the conditional format of the cells in the range based
> > upon the value in the cell to the RIGHT.
> >
> > Dim myRange As Range
> > Set myRange = Range("C1:C10")
> > With myRange
> > .FormatConditions.Delete
> > .FormatConditions.Add Type:=xlExpression, Formula1:= _
> > "=RC[1]=""PASS"""
> > .FormatConditions(1).Interior.ColorIndex = 4
> > .FormatConditions.Add Type:=xlExpression, Formula1:= _
> > "=RC[1]=""WATCH"""
> > .FormatConditions(2).Interior.ColorIndex = 6
> > .FormatConditions.Add Type:=xlExpression, Formula1:= _
> > "=RC[1]=""FAIL"""
> > .FormatConditions(3).Font.ColorIndex = 2
> > .FormatConditions(3).Interior.ColorIndex = 3
> > End With
> >
> > --
> > HTH,
> > Barb Reinhardt
> >
> > If this post was helpful to you, please click YES below.
> >
> >
> >
> > "Dee" wrote:
> >
> > > Hello, I have a worksheet whos data comes from SQL Server db. Autofilter is
> > > turned on. Within the worksheet are many columns. I would like to change
> > > the backcolor of the cells in column C based on the content of the
> > > cell in column D (could be "PASS", "WATCH", or "FAIL") on the same row
> > > (i.e. c1=5 d1='Pass' change backcolor of c1
> > > to Green, or c1=7 d1='WATCH' change backcolor to Yellow, or c1=10 d1='FAIL'
> > > change backcolor to Red). How do I identify the range and apply correct
> > > backcolor to cells within range? Need to be coded using vba macro? --
> > > DC
> > >
> > > --
> > > Dee

 
Reply With Quote
 
Dee
Guest
Posts: n/a
 
      6th Oct 2008
Thanks All, for replying. All were very good solutions. I decided to use
Gary's suggestion in my program, it was the easiest to follow. Others will
have to maintain code, it will take less time explaining to others. Thanks
again, you all are save me a lot of time.
--
DC


"FSt1" wrote:

> hi
> you will have to have some way of telling vb what range to apply the cf to.
> you could add an input box so that the code will ask you what range.
> Dim myRange As Range
> Dim r As String
> r = InputBox("enter range")
> Set myRange = Range(r) 'Range("C1:C10")
> With myRange
> .ect
>
> or if you don't want to be bothered by input, you could do this.
>
> Dim myRange As Range
> Dim r As Long
> r = Cells(Rows.Count, "C").End(xlUp).Row
> Set myRange = Range("C1:C" & r)
> with myrange
> .ect
> this way vb would just find the last row and use it in the range.
>
> regards
> FSt1
>
> "Dee" wrote:
>
> > Barb, is there a way to code the range for the statement "Set myRange =
> > Range("C1:C10") " my db will update each month and add rows. Dont want to
> > have to change code every month.
> >
> > Thanks,
> > DC
> >
> > --
> > DC
> >
> >
> > "Barb Reinhardt" wrote:
> >
> > > Try this. It will set the conditional format of the cells in the range based
> > > upon the value in the cell to the RIGHT.
> > >
> > > Dim myRange As Range
> > > Set myRange = Range("C1:C10")
> > > With myRange
> > > .FormatConditions.Delete
> > > .FormatConditions.Add Type:=xlExpression, Formula1:= _
> > > "=RC[1]=""PASS"""
> > > .FormatConditions(1).Interior.ColorIndex = 4
> > > .FormatConditions.Add Type:=xlExpression, Formula1:= _
> > > "=RC[1]=""WATCH"""
> > > .FormatConditions(2).Interior.ColorIndex = 6
> > > .FormatConditions.Add Type:=xlExpression, Formula1:= _
> > > "=RC[1]=""FAIL"""
> > > .FormatConditions(3).Font.ColorIndex = 2
> > > .FormatConditions(3).Interior.ColorIndex = 3
> > > End With
> > >
> > > --
> > > HTH,
> > > Barb Reinhardt
> > >
> > > If this post was helpful to you, please click YES below.
> > >
> > >
> > >
> > > "Dee" wrote:
> > >
> > > > Hello, I have a worksheet whos data comes from SQL Server db. Autofilter is
> > > > turned on. Within the worksheet are many columns. I would like to change
> > > > the backcolor of the cells in column C based on the content of the
> > > > cell in column D (could be "PASS", "WATCH", or "FAIL") on the same row
> > > > (i.e. c1=5 d1='Pass' change backcolor of c1
> > > > to Green, or c1=7 d1='WATCH' change backcolor to Yellow, or c1=10 d1='FAIL'
> > > > change backcolor to Red). How do I identify the range and apply correct
> > > > backcolor to cells within range? Need to be coded using vba macro? --
> > > > DC
> > > >
> > > > --
> > > > Dee

 
Reply With Quote
 
Gary Keramidas
Guest
Posts: n/a
 
      6th Oct 2008
i don't always use conditional formatting unless i'm going to change data
between runs.

so, if you run your query and run code, something like this should work.

Sub test()
Dim i As Long
Dim ws As Worksheet
Dim lastrow As Long

Set ws = Worksheets("Sheet1")
lastrow = ws.Cells(Rows.Count, "C").End(xlUp).Row

For i = 1 To lastrow
With ws.Range("D" & i)
Select Case UCase(.Value)
Case "FAIL"
.Offset(, -1).Interior.ColorIndex = 3
Case "WATCH"
.Offset(, -1).Interior.ColorIndex = 19
Case "PASS"
.Offset(, -1).Interior.ColorIndex = 35
End Select
End With
Next

End Sub

--


Gary

"Dee" <(E-Mail Removed)> wrote in message
news:F8221571-AA91-4CEE-A04D-(E-Mail Removed)...
> Hello, I have a worksheet whos data comes from SQL Server db. Autofilter
> is
> turned on. Within the worksheet are many columns. I would like to
> change
> the backcolor of the cells in column C based on the content of the
> cell in column D (could be "PASS", "WATCH", or "FAIL") on the same row
> (i.e. c1=5 d1='Pass' change backcolor of c1
> to Green, or c1=7 d1='WATCH' change backcolor to Yellow, or c1=10
> d1='FAIL'
> change backcolor to Red). How do I identify the range and apply correct
> backcolor to cells within range? Need to be coded using vba macro? --
> DC
>
> --
> Dee



 
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
Conditional formatting--different formatting depending on cell con Tammy S. Microsoft Excel Misc 3 30th Mar 2009 08:11 PM
Protect Cell Formatting including Conditional Formatting =?Utf-8?B?TWljayBKZW5uaW5ncw==?= Microsoft Excel Misc 5 13th Nov 2007 05:32 PM
conditional Formatting based on cell formatting Totom Microsoft Excel Worksheet Functions 1 20th Jan 2007 02:02 PM
conditional Formatting based on cell formatting Totom Microsoft Excel Worksheet Functions 0 15th Jan 2007 04:35 PM
Conditional formatting - on an empty cell condition in _another_ cell? StargateFan Microsoft Excel Programming 5 29th May 2006 08:56 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:35 AM.