Solved

Excel VBA Apply Conditional Formatting to a Row

Posted on 2011-09-20
8
293 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 50
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
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

627 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