2

I've tried Googeling but found no answers... very frustrated.

I'm trying to do this in Excel:

  1. Select cells B1 - B5 (or just B1)
  2. The select cell A1 (which is yellow in colour)
  3. Cells B1 - B5 (or just B1 if only B1 selected) also becomes yellow.

This is for a leaves (holiday) spreadsheet and there are different colours for different leave purposes (eg. sick, holiday, half day, etc). Every time someone highlights the cells on the spreadsheet calendar, they can just click on the corresponding colour to colour the selected cells. I am trying to replicate this same behaviour.

This was done by someone for our office's excel sheet but I cannot contact the original person who made. I'm trying to duplicate this function in a new excel sheet.

How do I do it? Macro? VB script?

6
  • Is there a reason why you aren't using the Fill Colour tool on the ribbon to make the cells yellow? Commented Feb 10, 2017 at 5:21
  • How about the Format painter option? Will that work for you?
    – Prasanna
    Commented Feb 10, 2017 at 8:16
  • I suppose that it is possible only with macro. Doesn't seem very handy: every even click will change color of cell(s)?
    – Merzavets
    Commented Feb 10, 2017 at 8:22
  • Answering your question, you should look at formula bar to determine is there any formula or something; also you should press Alt+F11 and look is there some macros attached which work as mouse click handler.
    – Merzavets
    Commented Feb 10, 2017 at 8:29
  • Yes I can use the fill colour tool but this is for a leaves (holiday) spreadsheet and there are different colours for different leave purposes (eg. sick, holiday, half day, etc). Everytime someone highlights the cells on the spreadsheet calendar, they can just click on the corresponding colour to colour the selected cells. I am trying to replicate this same behaviour. Commented Feb 10, 2017 at 8:37

2 Answers 2

0

You can only do this through VBA. Here is some code you can drop into the Sheet object.

Private lastRange As Range

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Address = Range("A1").Address Then
        If Not lastRange Is Nothing Then
            lastRange.Interior.Color = Range("A1").Interior.Color
        End If
    Else
       Set lastRange = Target
    End If
End Sub

The lastRange object is saved from selection to selection. This is the only way to have VBA remember what range we want to highlight. Whenever you change selection, it sets that selection to lastRange unless it's A1 in which case it colors the previous selection as whatever color A1 is..


Based on your comments, though, your need is a bit broader than this. Since you want a range of cells of various colors, this is probably the code you want:

Private lastRange As Range

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Cells.Count = 1 Then
        If Not Intersect(Target, Range("A1:A5")) Is Nothing Then
            If Not lastRange Is Nothing Then
                lastRange.Interior.Color = Target.Interior.Color
                Exit Sub
            End If
        End If
    End If
    Set lastRange = Target
End Sub

Now there's a whole range of cells A1:A5 that can be all different colors. If you select a single cell in that range, it will set your previous selection to be the same color as that current cell. You can adjust the A1:A5 range to be whatever you need.


This has one oddity that I know of: If you select a range, then a shape, then A1, the range you had selected before is still stored as lastRange. That's probably OK but I don't know your use case.

2
  • That's it! This is the perfect one. My apologies for the late reply. Thank you so much! Commented Feb 28, 2017 at 7:50
  • Glad it worked. Click the check mark next to my answer to mark it as accepted and close the question. Commented Feb 28, 2017 at 11:57
0

Open your excel file and in the HOME tab go to Conditional Formatting and click on manage rules. You should be able to see a rule related to the cells mentioned in your question.

enter image description here

Check the below microsoft link for more info:

How to use conditional formatting

1
  • Nope this is not what I am looking for. I am looking for something that enables clicking on a cell and it changes another previously selected cell's colour. Commented Feb 10, 2017 at 8:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .