Consulting

Results 1 to 8 of 8

Thread: Solved: Set Range depending on font colour

  1. #1

    Solved: Set Range depending on font colour

    Hi all,
    Is there a way to SET a range depending on font color ?
    I jusy want to work with cells in the range with font color green.
    What I got so far below. HELP!
    Thanks,
    [vba]
    Set BigRange = Range("q1:q20").Font.ColorIndex = 4 ‘ this is the problem
    If Range("D34").Value < 1 Then
    For Each C In Range("BigRange ").Cells
    If C.Value = WorksheetFunction.Small(Range("BigRange "), 1) Then
    Cells(C.Row, C.Offset(, -13).Column).Value = "YES"
    Range(C, C.Offset(, 13)).Font.ColorIndex = 3
    Exit For
    End If
    Next C
    End If
    [/vba]

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You would have to loop through the range, test the colour, and union all cells with that colour.

    As you loop through BigRange later, why not just loop the original range, and add a test before you set the value?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    thanks xld, will give that a try.

  4. #4
    Here’s what i came up with. its the best i could come up with, with my knowledge.
    If anyone has any ideas as to how to make it more efficient, please let me know.

    [vba]If Range("D34").Value < 1 Then
    For Each C In Range("Q9:Q29").Cells
    If C.Font.ColorIndex = 4 Then
    If RR Is Nothing Then
    Set RR = C
    Else
    Set RR = Application.Union(RR, C)
    End If
    End If
    Next C

    For Each C In RR.Cells
    If C.Value = WorksheetFunction.Small(RR, 1) Then
    Cells(C.Row, C.Offset(, -13).Column).Value = "YES"
    Range(C, C.Offset(, 13)).Font.ColorIndex = 3
    Exit For
    End If
    Next C
    End If[/vba]

  5. #5
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    [vba]
    Smallest = WorksheetFunction.Small(RR, 1)
    For Each C In RR.Cells
    If C.Value = Smallest Then
    [/vba]

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Not tested, but I was meaning something like this

    [vba]

    For Each C In Range("q1:q20")

    If .Font.ColorIndex = 4 Then

    If C.Value = WorksheetFunction.Min(Range("q1:q20")) Then

    C.Offset(, -13).Value = "YES"
    C.Offset(, -13).Font.ColorIndex = 3
    Exit For
    End If
    End If
    Next C
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    thanks for the update, i'll have a look at both suggestions,

    xld your code checks if value is smallest in the whole range, but i'm looking for the smallest of the green font values.

    i'll have a play see what i can come up with.

    thanks again,

  8. #8
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    [vbA]
    If Range("D34").Value < 1 Then
    For Each C In Range("Q9:Q29").Cells
    If C.Font.ColorIndex = 4 Then
    If RR Is Nothing Then
    Set RR = C
    ElseIf
    RR.Value > C.Value
    Set RR = C
    End If
    End IF
    Next C

    RR.Offset(, -13).Value = "YES"
    RR.Offset(, 13).Font.ColorIndex = 3
    [/vba]

Posting Permissions

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