+ Reply to Thread
Results 1 to 5 of 5

VB Code To Replace Conditional Formatting

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-07-2008
    Location
    sCOTLAND
    MS-Off Ver
    Office 2003 SP3
    Posts
    256

    VB Code To Replace Conditional Formatting

    I have a workbook that requires a VB code to help me out with Conditional Formatting. I use Office 2003 which is restricted to 3 conditions, I know there is an add in I can use that would help me do this but other people may use this that wont have the add in. I have decided to use VB if possible to get this done.

    I have a range of cells from B22 – T22 in these cells I will be putting codes, when these codes are put into the cells I would like the cells to shade a different colour depending on what code I use. Here is an example of what I mean.

    P = blue

    S = red

    HL = green

    ML = magenta

    FL = orange

    I may have a couple of more codes I will add at a latter time. Is it possible for VB code to do this?
    Last edited by JimmiOO; 03-20-2009 at 10:05 AM.

  2. #2
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983

    Re: VB Code To Replace Conditional Formatting

    You may need to change ColourIndex number to suit

    'These instructions pre typed & are worded to cater for the novice programmer
    'To install macro to correct location

    'Copy the macro
    'GoTo Excel
    'Select sheet this macro is to apply to
    'Right Click on Sheet Name Tab > select View Code
    'Paste macro into the Worksheet Module displayed

    Private Sub Worksheet_Change(ByVal Target As Range)
       If Not Application.Intersect(Target, Range("b22:t22")) Is Nothing Then
          Application.EnableEvents = False
          With Target.Interior
             Select Case UCase(Target.Value)
             Case "P"
                .ColorIndex = 3
             Case "S"
                .ColorIndex = 5
             Case "HL"
                .ColorIndex = 10
             Case "ML"
                .ColorIndex = 7
             Case "FL"
                .ColorIndex = 46
             End Select
          End With
       End If
       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.

  3. #3
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: VB Code To Replace Conditional Formatting

    Hi there,

    You may find the following code helpful:

    Option Explicit
    
    
    Const msRangeToMonitor  As String = "B22:T22"
    Const mlMagenta         As Long = &HFF00FF
    Const mlOrange          As Long = &H80FF&
    Const mlGreen           As Long = &HC000&
    Const mlBlue            As Long = &HFF0000
    Const mlRed             As Long = &HFF&
    
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    
        Dim lColour As Long
    
        If Target.Cells.Count = 1 And _
           Not Intersect(Target, Me.Range(msRangeToMonitor)) Is Nothing Then
    
           Select Case Target.Value
                  Case Is = "P"
                       Target.Interior.Color = mlBlue
                  Case Is = "S"
                       Target.Interior.Color = mlRed
                  Case Is = "HL"
                       Target.Interior.Color = mlGreen
                  Case Is = "ML"
                       Target.Interior.Color = mlMagenta
                  Case Is = "FL"
                       Target.Interior.Color = mlOrange
                  Case Else
                       Target.Interior.ColorIndex = xlColorIndexNone
           End Select
    
        End If
    
    End Sub
    The code should be inserted into the VBA code module of the worksheet in question (not into a standard VBA code module).

    All of the parameters (i.e. Range, Colours etc.) are defined as constants at the top of the module, and can easily be tweaked if necessary. The background colours are defined as COLOR values rather than COLORINDEX values (just in case you're not using the standard colour palette).

    You don't mention whether or not the cells may contain values other than those you've defined as needing to be highlighted, but in any event, the "Case Else" condition caters for the situation where the value of a cell changes from (e.g.) S to a value not included in your list. In such a case, the cell background colour will change from Red to no colour (clear).

    Hope this helps - please let me know how you get on.

    Regards,

    Greg M

  4. #4
    Forum Contributor
    Join Date
    07-07-2008
    Location
    sCOTLAND
    MS-Off Ver
    Office 2003 SP3
    Posts
    256

    Re: VB Code To Replace Conditional Formatting

    Thanks alot guys i have tried both and they both work well, just what i needed, i have went with Gregs one this time as the Case Else will be needed, all cells will contain an X before i add my code, in that case i need it to have no colour.

    Thanks again.

  5. #5
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: VB Code To Replace Conditional Formatting

    Hi again,

    Many thanks for your feedback - I'm glad I was able to help.

    Regards,

    Greg M

+ 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