Solved

Excel VBA Apply Conditional Formatting to a Row

Posted on 2011-09-20
8
287 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
 
LVL 50

Expert Comment

by:teylyn
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 50

Expert Comment

by:teylyn
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
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 …
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

706 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now