+ Reply to Thread
Results 1 to 4 of 4

looping across columns in range?

  1. #1
    Amy
    Guest

    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. #2
    Trevor Shuttleworth
    Guest

    Re: looping across columns in range?

    See the solution to your similar post in Excel.Programming

    Regards

    Trevor


    "Amy" <[email protected]> wrote in message
    news:[email protected]...
    > 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. #3
    Matt Lunn
    Guest

    RE: looping across columns in range?

    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. #4
    Matt Lunn
    Guest

    RE: looping across columns in range?

    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 to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1