Solved

VBA to change colors

Posted on 2012-03-14
4
476 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
  • 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

828 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