Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Excel VBA Apply Conditional Formatting to a Row

Posted on 2011-09-20
8
Medium Priority
?
295 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 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

715 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