+ Reply to Thread
Results 1 to 5 of 5

VBA code for conditional formatting

Hybrid View

  1. #1
    Registered User
    Join Date
    01-03-2011
    Location
    Bangalore, India
    MS-Off Ver
    MS Excel 2010
    Posts
    49

    VBA code for conditional formatting

    Hi,

    I have a data from cell C4:Fxxxx (the last row is defined as xxxx because, each time the row gets chagned, it's not constant).
    I am looking for some assistance on how to define formula for conditional formatting.

    If cell value of F4 <=500000, corresponding cell (ie C4 in this situation) should be green color, similarly it should do for all cells in that column.

    I have written the following code, but with no luck. Any assistance!!!

    Here is my code

    Sub Test()
    Dim ws As Worksheet
        Dim lRow As Long
        Set ws = ThisWorkbook.Sheets("Sheet1")
        With ws
            lRow = .Range("D" & .Rows.Count).End(xlUp).Row
            .Range("E4:E" & lRow).Formula = "=If(D4>=0,0,D4)"
            .Range("E4:E" & lRow).Value = .Range("E4:E" & lRow).Value
            .Range("F4:F" & lRow).Formula = "=If(E4<>0,ABS(sum(E$3:E4)),"""")"
            .Range("F4:F" & lRow).Value = .Range("F4:F" & lRow).Value
            If (Range("F4:F" & lRow).Value) <= 500000 Then
            With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .ThemeColor = xlThemeColorDark2
            .TintAndShade = -9.99481185338908E-02
            .PatternTintAndShade = 0
        End With
        Else
        Range("A1").Select
        End If
        End With
    End Sub
    Thanks!
    Last edited by Srikanth H N; 03-03-2014 at 10:15 PM.

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,936

    Re: VBA code for conditional formatting

    Why not just apply the Conditional Formatting directly to column C? You can also use the macro recorder to capture to code and add it to your macro.
    Ben Van Johnson

  3. #3
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: VBA code for conditional formatting

    You cannot apply an if statement to a range consisting of more than 1 cell. You need to incorporate a loop for that. Something like this (untested):

    Sub Test()
    Dim ws As Worksheet
        Dim lRow As Long
        Dim rCell As Range
        Set ws = ThisWorkbook.Sheets("Sheet1")
        With ws
            lRow = .Range("D" & .Rows.Count).End(xlUp).Row
            .Range("E4:E" & lRow).Formula = "=If(D4>=0,0,D4)"
            .Range("E4:E" & lRow).Value = .Range("E4:E" & lRow).Value
            .Range("F4:F" & lRow).Formula = "=If(E4<>0,ABS(sum(E$3:E4)),"""")"
            .Range("F4:F" & lRow).Value = .Range("F4:F" & lRow).Value
            
            For Each rCell In .Range("F4:F" & lRow)
            If rCell.Value <= 500000 Then
                With rCell.Interior
                .Pattern = xlSolid
                .PatternColorIndex = xlAutomatic
                .ThemeColor = xlThemeColorDark2
                .TintAndShade = -9.99481185338908E-02
                .PatternTintAndShade = 0
                End With
            End If
            Next rCell
            
        End With
    End Sub

  4. #4
    Registered User
    Join Date
    01-03-2011
    Location
    Bangalore, India
    MS-Off Ver
    MS Excel 2010
    Posts
    49

    Re: VBA code for conditional formatting

    Thank you... By using the above code i am having the conditioanl formatting in column F. But i want the formatting to happen in COlumn D based on values in column F.

  5. #5
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: VBA code for conditional formatting

    Use the offset feature

    With rCell.Offset(0, -2).Interior 'rCell is column F.  Offset -2 columns for column D

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. VBA code to allow more than 3 conditional formatting
    By DannyJ in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-24-2012, 10:45 AM
  2. Conditional Formatting Code - Deletes Wrong Conditional Format
    By RSpecianJr in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-29-2010, 10:53 AM
  3. Conditional Formatting with code
    By rlcohen70 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-14-2008, 04:51 AM
  4. Conditional formatting code please
    By Cath in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-21-2007, 06:34 PM
  5. Conditional Formatting & VBA Code
    By Sharp in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-05-2007, 06:30 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1