+ Reply to Thread
Results 1 to 4 of 4

VBA based conditional formatting (>3 formats needed)

Hybrid View

  1. #1
    Registered User
    Join Date
    10-15-2004
    Posts
    2

    VBA based conditional formatting (>3 formats needed)

    Good Morning/Afternoon/Evening

    I'm looking to conditionally format the cell colour of an entire row based on text entered in the last cell of the row. Excel's built-in conditional formatting is beautiful for this but I need six different formats (and three seems to be the max).

    I did a search and found this helpful thread and I'm trying to adapt the code to my needs.

    Here's what I've got so far.

    Private Sub Worksheet_Change(ByVal Target As Range)
    
       Dim Rng As Range
       Dim RowRng As Range
       Dim iColour As Integer
       
       MsgBox "Macro has been executed!!!!"
    
       Set RowRng = Range(Rng, Range("Rng").Offset(0, -9))
       
       For Each Rng In Target
          
          If Not Application.Intersect(Rng, Range("J1:J100")) Is Nothing Then
             Application.EnableEvents = False
             Select Case Rng.Value
                 Case Is = "ahead"
                    iColour = 3
                 Case Is = "on time"
                    iColour = 45
                 Case Is = "behind"
                    iColour = 50
                 Case Else
                    iColour = 0
             End Select
             If iColour = 0 Then
                RowRng.Interior.ColorIndex = xlNone
             Else
                RowRng.Interior.ColorIndex = iColour
             End If
          End If
       Next Rng
       Application.EnableEvents = True
    End Sub
    At one point in time, when I had set RowRng to a static range, the macro would successfully change the row colour whenever I changed a cell in J1:J100.

    I then set out to modify my code to change only the row of the selected cell in the J column and came up with this part.

    Set RowRng = Range(Rng, Range("Rng").Offset(0, -9))
    I was getting an error at this line, but now all of a sudden for some reason, the macro isn't even getting executed when I enter text in the J column of the workseet. I know this because it isn't showing my message box when I change a value in the J column.

    What gives??

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Remove the speech marks around the second rng.

    You also appear to only have 4 conditions which could be handled without VBA, use the original cell colour as the baseis, then change for each of your 3 conditions
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    The reason that the macro is no longer working is that it crashed or was stopped after this line of code was run
    Application.EnableEvents = False
    and before this line of code was run
    Application.EnableEvents = True
    Place this macro on the same module sheet as your Worksheet Change macro and run it manually

    Sub ReEnableEvents()
    Application.EnableEvents = True
    End Sub
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

  4. #4
    Registered User
    Join Date
    10-15-2004
    Posts
    2
    Mudraker: That works! Cheers mate.

    Roy: I left out some other formats for the sake of shortening the code to pasted here. I'm actually using 7 different formats.

    I fixed the quotes and it was still throwing errors so I poked around in the debugger and realized Rng was null until inside the for loop. So I moved that line of code. Here's the code that works for me.

    Private Sub Worksheet_Change(ByVal Target As Range)
       
       Dim Rng As Range
       Dim RowRng As Range
       Dim iColour As Integer
     
       For Each Rng In Target
          
          Set RowRng = Range(Rng.Offset(0, -9), Rng)
          
          If Not Application.Intersect(Rng, Range("J1:J100")) Is Nothing Then
                    
             Application.EnableEvents = False
             
             Select Case Rng.Value
                Case Is = "ahead"
                   iColour = 3
                Case Is = "on time"
                   iColour = 45
                Case Is = "behind"
                   iColour = 50
                Case Else
                   iColour = 0
             End Select
             
             If iColour = 0 Then
                RowRng.Interior.ColorIndex = xlNone
             Else
                RowRng.Interior.ColorIndex = iColour
             End If
             
          End If
          
       Next Rng
       
       Application.EnableEvents = True
       
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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