Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Amy
 
Posts: n/a
Default looping across columns in range?

Can someone help me here, we have the following VBA code:

Sub TrafficLight()
Dim R As Integer
Dim Pcent As Double
Pcent = 0.5
For R = 9 To 384 ' note the number range
If Range("BF" & R).Value = "-" Then
Range("BF" & R).Interior.Color = vbWhite
' Greater than 5% less
Else
If Range("BF" & 385).Value * (1 + Pcent) < Range("BF" & R).Value Then
Range("BF" & R).Interior.Color = vbRed
Else
' greater than 5% more
If Range("BF" & 385).Value Range("BF" & R).Value * (1 + Pcent) Then
Range("BF" & R).Interior.Color = vbGreen
Else: Range("BF" & R).Interior.Color = vbWhite
End If
End If
End If
Next R
End Sub

It is working fine, however, as you can see it only works for range BF
at the moment, when I want it to run the same calculations from column
BF to column CJ.

Al;l help gratefully received.

Scott

  #2   Report Post  
Trevor Shuttleworth
 
Posts: n/a
Default

See the solution to your similar post in Excel.Programming

Regards

Trevor


"Amy" wrote in message
oups.com...
Can someone help me here, we have the following VBA code:

Sub TrafficLight()
Dim R As Integer
Dim Pcent As Double
Pcent = 0.5
For R = 9 To 384 ' note the number range
If Range("BF" & R).Value = "-" Then
Range("BF" & R).Interior.Color = vbWhite
' Greater than 5% less
Else
If Range("BF" & 385).Value * (1 + Pcent) < Range("BF" & R).Value Then
Range("BF" & R).Interior.Color = vbRed
Else
' greater than 5% more
If Range("BF" & 385).Value Range("BF" & R).Value * (1 + Pcent) Then
Range("BF" & R).Interior.Color = vbGreen
Else: Range("BF" & R).Interior.Color = vbWhite
End If
End If
End If
Next R
End Sub

It is working fine, however, as you can see it only works for range BF
at the moment, when I want it to run the same calculations from column
BF to column CJ.

Al;l help gratefully received.

Scott



  #3   Report Post  
Matt Lunn
 
Posts: n/a
Default

Hi Scott,

Have you tried conditional formatting?

Thanks,
Matt

"Amy" wrote:

Can someone help me here, we have the following VBA code:

Sub TrafficLight()
Dim R As Integer
Dim Pcent As Double
Pcent = 0.5
For R = 9 To 384 ' note the number range
If Range("BF" & R).Value = "-" Then
Range("BF" & R).Interior.Color = vbWhite
' Greater than 5% less
Else
If Range("BF" & 385).Value * (1 + Pcent) < Range("BF" & R).Value Then
Range("BF" & R).Interior.Color = vbRed
Else
' greater than 5% more
If Range("BF" & 385).Value Range("BF" & R).Value * (1 + Pcent) Then
Range("BF" & R).Interior.Color = vbGreen
Else: Range("BF" & R).Interior.Color = vbWhite
End If
End If
End If
Next R
End Sub

It is working fine, however, as you can see it only works for range BF
at the moment, when I want it to run the same calculations from column
BF to column CJ.

Al;l help gratefully received.

Scott


  #4   Report Post  
Matt Lunn
 
Posts: n/a
Default

Try this...


Dim C As Integer
Dim R As Integer
Dim Pcent As Double
Pcent = 0.5

For C = Range("BF1").Column To Range("CJ1").Column 'Can change columns to
include from here
For R = 9 To 384 ' note the number range


If Cells(R, C).Value = "-" Then
Cells(R, C).Interior.Color = vbWhite
' Greater than 5% less
Else
If Cells(R, C).Value * (1 + Pcent) < Cells(R, C).Value Then
Cells(R, C).Interior.Color = vbRed
Else
' greater than 5% more
If Cells(R, C).Value Cells(R, C).Value * (1 + Pcent) Then
Cells(R, C).Interior.Color = vbGreen
Else: Cells(R, C).Interior.Color = vbWhite
End If
End If
End If
Next R
Next C


HTH,
Matt

"Amy" wrote:

Can someone help me here, we have the following VBA code:

Sub TrafficLight()
Dim R As Integer
Dim Pcent As Double
Pcent = 0.5
For R = 9 To 384 ' note the number range
If Range("BF" & R).Value = "-" Then
Range("BF" & R).Interior.Color = vbWhite
' Greater than 5% less
Else
If Range("BF" & 385).Value * (1 + Pcent) < Range("BF" & R).Value Then
Range("BF" & R).Interior.Color = vbRed
Else
' greater than 5% more
If Range("BF" & 385).Value Range("BF" & R).Value * (1 + Pcent) Then
Range("BF" & R).Interior.Color = vbGreen
Else: Range("BF" & R).Interior.Color = vbWhite
End If
End If
End If
Next R
End Sub

It is working fine, however, as you can see it only works for range BF
at the moment, when I want it to run the same calculations from column
BF to column CJ.

Al;l help gratefully received.

Scott


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
How do I create a schedule from a list of dates ? Gavin Morris Charts and Charting in Excel 2 October 28th 10 04:08 PM
Can a formula check for a certain value in a range? Lee IT Excel Discussion (Misc queries) 3 April 8th 05 07:36 AM
extract data from a range of cells in rows or columns when a date. Dartyon Excel Worksheet Functions 0 February 24th 05 10:37 PM
Dynamic Print Range Help waxwing Excel Worksheet Functions 2 February 21st 05 03:47 PM
Define a range based on another named range Basil Excel Worksheet Functions 2 February 21st 05 01:47 PM


All times are GMT +1. The time now is 12:03 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"