Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I format a range of cells based upon another cell value

Status
Not open for further replies.

dab1477

Technical User
Mar 4, 2011
33
US
Thanks in advance for the assist to a VBA newbie. I want to perform the following:

I want to color format cells A1:k:1 based upon the value entered into K1. I want to do this for each of 65 successive rows.
I can do this via conditional formatting for each series of cells, but I want to do this in one fell swoop using code.
My current conditional formatting statement looks like this for cell K1: $A$1="kaizen" then cells A1:k1 will format as fill in red with black font. I would like this to occur via VBA code. I don't know how to do this.
 
Hi,

You will not need VBA. Use Excel Conditional Formatting Wizard:

1) select ALL cells you want to format conditionally in ALL rows

2) open the CF wizard and use enter a formula option or something similar, don't have my Excel for reference.

3) enter this formula: note that ANY CF formula is entered with respect to the TOP ROW of you selection.
[tt]
=$K1="Kaizen"
[/tt]
4) select the FORMAT you want


Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Thanks. I was able to do that with CF. I was attempting to learn if VBA could do this. You did help me with the cell ref issue. Thanks.
 
I was attempting to learn if VBA could do this
The macro recorder is your friend.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Code:
Dim rng as range, r as range

Set rng = ActiveSheet.usedrange

For each r in range(cells(1,1), cells(1,1).end(xldown))
   With intersect(r.entirerow, rng)
      If cells(r.row, "k").value = "Kaizen" then
          .interior.color = vbred
          .font.color = vbwhite
      Else
          .interior.color = vbwhite
          .font.color = vbblack
      End if
   End with
Next

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
You can use events to trace user actions. For changes in cell contents (i.e. formulas in cells, not their calculation results), add the code in worksheet's module:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Long
With Me
    If Not Intersect(Target, .Range("K1:K65")) Is Nothing Then
        r = Target.Row
        If .Range("A" & r).Value = "kaizen" Then
            .Range("A" & r & ":K" & r).Font.Color = vbRed
        Else
            .Range("A" & r & ":K" & r).Font.Color = vbBlack
        End If
    End If
End With
End Sub
Note that the code works for single cell changed. For values pasted in multiple cells the above code will pick the first row of changed range.

combo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top