Solved

VBA to change colors

Posted on 2012-03-14
4
502 Views
Last Modified: 2012-03-15
Hi Guys, what would be the code for

If Range(S6:S450) >=38.75 then VB red, else VB white

If (S6:S450) >=38.75 then Range(B6:B450)VB red, else VB white

I need the code to activate these 2 columns depending on Column S values

I did this with conditional format but it slows down my sheet operation.
0
Comment
Question by:rsen1
[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
4 Comments
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 37723117
It's unlikely that VBA will be faster than Conditional Formatting.  Indeed, it would probably be slower to use VBA with a Change or Calculate event sub than to just use CF.
0
 

Author Comment

by:rsen1
ID: 37723123
Thank you for your response, but would you please help me with code all the same?
0
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 37723159
I still think that this is a bad idea, but if you insist, this goes in the sheet module for the worksheet you need to "watch":


Option Explicit

Private Sub Worksheet_Calculate()
    
    ApplyColors
    
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
    
    If Not Intersect(Target, Me.[s6:s450]) Is Nothing Then
        ApplyColors Intersect(Target, Me.[s6:s450])
    End If
    
End Sub

Private Sub ApplyColors(Optional rng As Range)
    
    Dim UseThisRange As Range
    Dim cel As Range
    
    Const Threshold As Double = 38.75
    
    Application.EnableEvents = False
    
    If Not rng Is Nothing Then
        Set UseThisRange = rng
    Else
        Set UseThisRange = Intersect(Me.UsedRange, Me.[s6:s450])
    End If
    
    For Each cel In UseThisRange.Cells
        If cel >= Threshold Then
            cel.Interior.Color = vbRed
            Me.Cells(cel.Row, "b").Interior.Color = vbRed
        Else
            cel.Interior.ColorIndex = xlColorIndexNone
            Me.Cells(cel.Row, "b").Interior.ColorIndex = xlColorIndexNone
        End If
    Next
    
    Application.EnableEvents = True
    
End Sub

Open in new window



Keep the Change event if the values in S will change by manual entry, and the Calculate event if they will change by formula recalculation.
0
 

Author Comment

by:rsen1
ID: 37723188
Thank you very much
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

734 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