Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Excel VBA Format color of cells changed on sheet change event

Posted on 2014-10-27
4
Medium Priority
?
904 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:Murray Brown
  • 2
  • 2
4 Comments
 
LVL 53

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:Murray Brown
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 53

Accepted Solution

by:
Rgonzo1971 earned 2000 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:Murray Brown
ID: 40410438
Thanks very much
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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!
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

916 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