+ Reply to Thread
Results 1 to 5 of 5

VBA code for conditional formatting

Hybrid View

  1. #1
    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

  2. #2
    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.

+ 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