Solved

Excel VBA Format color of cells changed on sheet change event

Posted on 2014-10-27
4
707 Views
Last Modified: 2014-10-29
Hi
I am using the code given to me in a previous expert post  to change all cells
with the same value as the cell being changed.
How do I also change the formatting of all these changed cells (other than the target cell) to be the same
background color

Private PrevValue As Variant

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column = 2 Then 'column B
        Application.EnableEvents = False
        Target.EntireColumn.Replace PrevValue, Target.Value, xlWhole
        PrevValue = Target.Value
        Application.EnableEvents = True
    End If
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    PrevValue = Target.Value
End Sub
0
Comment
Question by:murbro
  • 2
  • 2
4 Comments
 
LVL 48

Expert Comment

by:Rgonzo1971
ID: 40407796
Hi,

Which format should the target cell and which format should the remaining column have?

Regards
0
 

Author Comment

by:murbro
ID: 40407799
I want the target cell to have a background color of orange and all the other cells to have no background color (ie set back to no color)
0
 
LVL 48

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 40407818
Hi,


pls try

Private PrevValue As Variant

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column = 2 Then 'column B
        Application.EnableEvents = False
        With Application.ReplaceFormat.Interior
            .Pattern = xlNone
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
        Target.EntireColumn.Replace PrevValue, Target.Value, xlWhole, , , , , True
        Target.Interior.Color = 49407
        PrevValue = Target.Value
        Application.EnableEvents = True
    End If
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    PrevValue = Target.Value
End Sub

Open in new window

Regards
0
 

Author Closing Comment

by:murbro
ID: 40410438
Thanks very much
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
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…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now