Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 172
Default conditional format - put inside a for/next loop

I would like to conditional format some cells all in the same column
with the following:

Cells(31,4).Select
Selection.FormatConditions.Delete
Selection.FormatConditions.Add Type:=xlExpression,
Formula1:="=C31=1"
Selection.FormatConditions(1).Interior.ColorIndex = 38

But I want to put this piece of code inside a For/Next loop to change
a large number of rows. I think I know how to change the first line
but I need to know how to change formula statement so it will work
within the loop. (The fill color of column D is determined by the
value of column C cell in the same row.)

Using excel 2003/WinXP


John Keith

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,986
Default conditional format - put inside a for/next loop

This might work"
Sub liminal()
Dim c As Range
i = 31
For Each c In Range("D31:D500") 'Change Range size to suit
c.FormatConditions.Add Type:=xlExpression, _
Formula1:="=C" & i & "= 1"
c.FormatConditions(1).Interior.ColorIndex = 38
i = i + 1
Next
End Sub

"John Keith" wrote:

I would like to conditional format some cells all in the same column
with the following:

Cells(31,4).Select
Selection.FormatConditions.Delete
Selection.FormatConditions.Add Type:=xlExpression,
Formula1:="=C31=1"
Selection.FormatConditions(1).Interior.ColorIndex = 38

But I want to put this piece of code inside a For/Next loop to change
a large number of rows. I think I know how to change the first line
but I need to know how to change formula statement so it will work
within the loop. (The fill color of column D is determined by the
value of column C cell in the same row.)

Using excel 2003/WinXP


John Keith


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 172
Default conditional format - put inside a for/next loop

On Thu, 27 Nov 2008 19:53:01 -0800, JLGWhiz
wrote:

This might work"
Sub liminal()
Dim c As Range
i = 31
For Each c In Range("D31:D500") 'Change Range size to suit
c.FormatConditions.Add Type:=xlExpression, _
Formula1:="=C" & i & "= 1"
c.FormatConditions(1).Interior.ColorIndex = 38
i = i + 1
Next
End Sub


Your exact suggestion above did not work perfectly (see below) but the
concept of doing the string substitution did work and I think I can
adjust to meet my requirements.

What didn't work:
- format in column D was controlled by value in column F rather than C
- increment on i wasn't right, row 34 controlled row 32

I have no idea on why this behavior but then I'm still a beginner.

Thanks for teaching me the concept of the string replacement though!!!


John Keith

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default conditional format - put inside a for/next loop

One way:

Range("D31:D100").Select
With Selection.FormatConditions
.Delete
.Add Type:=xlExpression, Formula1:="=C31=1"
.Item(1).Interior.ColorIndex = 30
End With



In article ,
John Keith wrote:

I would like to conditional format some cells all in the same column
with the following:

Cells(31,4).Select
Selection.FormatConditions.Delete
Selection.FormatConditions.Add Type:=xlExpression,
Formula1:="=C31=1"
Selection.FormatConditions(1).Interior.ColorIndex = 38

But I want to put this piece of code inside a For/Next loop to change
a large number of rows. I think I know how to change the first line
but I need to know how to change formula statement so it will work
within the loop. (The fill color of column D is determined by the
value of column C cell in the same row.)

Using excel 2003/WinXP


John Keith

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 172
Default conditional format - put inside a for/next loop

On Thu, 27 Nov 2008 21:08:27 -0700, JE McGimpsey
wrote:

One way:

Range("D31:D100").Select
With Selection.FormatConditions
.Delete
.Add Type:=xlExpression, Formula1:="=C31=1"
.Item(1).Interior.ColorIndex = 30
End With


JE,

Thanks for teaching me how to apply this format to a range.

But you missed a key point in my OP, I needed the "=C31" to change for
each row, e.g. "=C32", "=C33" and so on.

Thank you for looking at my inquiry.



John Keith



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default conditional format - put inside a for/next loop

In article ,
John Keith wrote:

Thanks for teaching me how to apply this format to a range.

But you missed a key point in my OP, I needed the "=C31" to change for
each row, e.g. "=C32", "=C33" and so on.


You should try a solution before you decide it doesn't work...
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 172
Default conditional format - put inside a for/next loop

On Fri, 28 Nov 2008 04:10:02 -0700, JE McGimpsey
wrote:

In article ,
John Keith wrote:

Thanks for teaching me how to apply this format to a range.

But you missed a key point in my OP, I needed the "=C31" to change for
each row, e.g. "=C32", "=C33" and so on.


You should try a solution before you decide it doesn't work...


Like I noted in another response, I'm a beginner. Sure enough it works
but I don't have a clue why the reference is correct for each row.

Thanks for pointing this out.

PS - But now you've added to my confusion. :-)


John Keith

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default conditional format - put inside a for/next loop


In article ,
John Keith wrote:

On Fri, 28 Nov 2008 04:10:02 -0700, JE McGimpsey
wrote:

In article ,
John Keith wrote:

Thanks for teaching me how to apply this format to a range.

But you missed a key point in my OP, I needed the "=C31" to change for
each row, e.g. "=C32", "=C33" and so on.


You should try a solution before you decide it doesn't work...


Like I noted in another response, I'm a beginner. Sure enough it works
but I don't have a clue why the reference is correct for each row.


It works the same way as entering the formula in the XL user interface.
Entering a relative address will cause XL to enter the CF relative to
the cells in the range.

FWIW, being a beginner is among the worst of excuses for not trying
something. Trying, and attempting to figure out why it works, is one of
the best ways to quickly get beyond beginner status!
  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 172
Default conditional format - put inside a for/next loop

On Thu, 27 Nov 2008 20:11:08 -0700, John Keith wrote:

I would like to conditional format some cells all in the same column
with the following:

Cells(31,4).Select
Selection.FormatConditions.Delete
Selection.FormatConditions.Add Type:=xlExpression,
Formula1:="=C31=1"
Selection.FormatConditions(1).Interior.ColorIndex = 38

But I want to put this piece of code inside a For/Next loop to change
a large number of rows. I think I know how to change the first line
but I need to know how to change formula statement so it will work
within the loop. (The fill color of column D is determined by the
value of column C cell in the same row.)


Before I got the 2 responses I was thinking about my question and I
wonder if this technique would have worked:

- set the conditional format in row 31
- copy cell D31
- paste special formats into other cells in column D where I needed it

I assume the cell reference in the formula would have been updated to
correctly refer to the adjacent cell in the row.


John Keith

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
recalculating RAND() inside a VBA loop vbaRay Excel Programming 5 September 25th 08 02:59 AM
Can an array be redim inside a for loop? kcenac Excel Programming 2 April 19th 08 06:46 PM
form inside a loop matt Excel Programming 2 February 26th 07 05:54 PM
Increment value inside conditional format Bill Burns Excel Programming 3 June 19th 04 08:25 PM
Conditional format cell ranges with loop? welded Excel Programming 7 June 19th 04 07:54 AM


All times are GMT +1. The time now is 10:14 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"