Solved

Excel VBA Apply Conditional Formatting to a Row

Posted on 2011-09-20
8
291 Views
Last Modified: 2012-05-12
Thank you for looking at my question,

I use the vba code below to grab data from an Excel workbook.

The number of rows will vary but there will always only ever be four columns.

For each row I would like to cell background colour in columns 1,2,3 and 4 to turn red if the cell value in cell 4 is greater than 6.

How can I achieve this please?
Sub Get_Data()
Dim wbk As Workbook
Dim destWbk As Workbook
        
    Set destWbk = ActiveWorkbook
    FileToOpen = "K:\Working Data\Loading.xls"
    
    Set wbk = Workbooks.Open(FileToOpen)
    
    wbk.Sheets(1).Range("A1:IV65536").Copy Destination:=destWbk.Sheets("TotalReq_vs_Bars").Range("A1")
    wbk.Close

End Sub

Open in new window

0
Comment
Question by:Crxfrd
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 36566196
Do you mean you want columns A:D to turn red if the value in column D of that row is >6?
0
 
LVL 24

Accepted Solution

by:
StephenJR earned 500 total points
ID: 36566206
Perhaps like this (Excel 2003)?
With Sheets("TotalReq_vs_Bars").Range("A1").CurrentRegion
    .FormatConditions.Delete
    .FormatConditions.Add Type:=xlExpression, Formula1:="=$D1>6"
    .FormatConditions(1).Interior.ColorIndex = 3
End With

Open in new window

0
 

Author Comment

by:Crxfrd
ID: 36566207
> rorya,

Sorry, that's exactly what I mean
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 50

Expert Comment

by:Ingeborg Hawighorst
ID: 36566220
Hello,

What version of Excel? Based on the .xls extension, I assume you're using 2003 or earlier. In that case something along the lines of

    destWbk.Range("D1").Select
    With destWbk.Columns("A:D")
       .FormatConditions.Delete
       .FormatConditions.Add Type:=xlExpression, Formula1:="=$D1>6"
       .FormatConditions(1).Interior.ColorIndex = 3
    End With

Open in new window

0
 
LVL 50

Expert Comment

by:Ingeborg Hawighorst
ID: 36566225
Sorry, StephenJR, slow fingers ...
0
 
LVL 24

Expert Comment

by:StephenJR
ID: 36566227
Sorry, don't think that will work, will have to come back if needed.
0
 

Author Closing Comment

by:Crxfrd
ID: 36566246
Excellent, Thank you
0
 
LVL 5

Expert Comment

by:DerZauberer
ID: 36566257
    Range("A1:D1").Select ' Sel 4 Cols 1st Row
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=$D1>6" ' Col 4 > 6
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 255 ' Red Bkgnd
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    Selection.Copy
    Range("A2:D65536").Select
    Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False ' Copy Formats to other rows
    Application.CutCopyMode = False

Open in new window

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question