Macro VBA/Conditional Formatting - click a cell and autofill with color in other cells from range ?

realest25

New Member
Joined
Jun 7, 2022
Messages
18
Office Version
  1. 2019
Platform
  1. Windows
Hello dear members. Sorry, maybe it's a bit confusing to understand from the title what I'm asking you.

I want to create a small statistics tool.

If I click or double click on any cell with number, in this example the number 21, after I click on the number 21, to be automatically filled with green color the number 21 and also 9 neighbors to its left and 9 neighbors to his right

I want this to be possible for all numbers from 1 to 36.

After clicking on any number, to be changed the color of the clicked cell and also in 9 neighbors to its left and 9 neighbors to its right.

All cells that are empty (with no number) are of no importance, only cells with numbers are important.

Any number that is clicked to be able to highlight itself also 9 neighbors to its left and 9 neighbors to its right.

The needed color is green color from this excel.

Is it possible ?



Test Tool.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAAB
1
2119220321422523119220321422523
3
4366366
5
618241824
7
8357357
9
1017251725
11
12348348
13
1416261626
15
16339339
17
1815271527
19
203214311330122911281032143113301229112810
21
22
Sheet1
 

Attachments

  • Untitled.png
    Untitled.png
    20.1 KB · Views: 4

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Possible by using SelectionChange event. The test would be to determine if the clicked cell contains a number and if so, colour the interior of the range that is 9 to the left and 9 to the right. If your picture is supposed to show the desired result, the description and the picture don't seem to agree.
Also, this sort of thing is more complicated than how I described it because if you click on 3 then 21, what should happen to what was 9 to the left and went green but is now 10 to the left?
 
Upvote 0
Possible by using SelectionChange event. The test would be to determine if the clicked cell contains a number and if so, colour the interior of the range that is 9 to the left and 9 to the right. If your picture is supposed to show the desired result, the description and the picture don't seem to agree.
Also, this sort of thing is more complicated than how I described it because if you click on 3 then 21, what should happen to what was 9 to the left and went green but is now 10 to the left?
Thank you for your reply. SelectionChange event. I understand. I am just a rookie. I don't know how to create it.
I hope, if possible, if I click 3 then 21, to not be 10 to left and 10 to left but to be 9 lef and 9 right.
Maybe somehow, every time a new cell is clicked, the previous highlight to be cancelled and to be created a new highlight selection of 9 to left and 9 to right of the clicked cell ?
 
Upvote 0
I would undo the whole row then apply the colour based on the target address. That is a value that the selectionChange event returns. Might have time to play, but will have to consider how to deal with the possibility that a cell with a number is clicked on and there are not 9 cells to the left or right of it. If that cannot be the case, it would be simpler to code.
 
Upvote 0
how to deal with the possibility that a cell with a number is clicked on and there are not 9 cells to the left or right of it. If that cannot be the case, it would be simpler to code.
Waiting for a comment on that situation.
 
Upvote 0
Waiting for a comment on that situation.
All I can say is that not all numbers that will be clicked, not all of them have 9 cells to the left and 9 to the right.
Maybe, if I can receive some kind of basic code, I will try to modify it afterwards.
All I need is the basic. I don't know how to create the basic vba/macro/SelectionChange.
Maybe it will be a very big help for me if I will receive an example of a code for just a number or two, with 3 cells to the left and 3 to the right.
After that I will try and adapt it according to my preferences.
 
Upvote 0
OK; this will raise an error if the range cannot be set to 9 left and 9 right in the row of the clicked cell.
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rng As Range

If Application.WorksheetFunction.IsNumber(Target) Then
   Range(Cells(Target.Row, 1), Cells(Target.Row, 50)).Interior.ColorIndex = -4142
   Set rng = Range(Cells(Target.Row, Target.Column).Offset(0, -9), Cells(Target.Row, Target.Column).Offset(0, 9))
   rng.Interior.ColorIndex = 4
End If

End Sub
You could trap for this error, but seeing as how the number would be 1004 (I think) and there are umpteen messages for that number, I don't know if I would. I'd first try to determine if there are 9 cells to the right and 9 to the left of the selected cell. That test would probably be ok if 1004 error was raised since it would be specific to trying to determine that.

The rng object as I've utilized it may not be required for so simple an operation, but I coded for it because I thought there might be more that had to be done with it in terms of testing for 9 left and 9 right. Note: I used 50 as a test. That number is the number of columns in the row that I thought might come into play. You could adjust that number, or test for the last column with data in the clicked row instead.
 
Upvote 0
Solution
OK; this will raise an error if the range cannot be set to 9 left and 9 right in the row of the clicked cell.
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rng As Range

If Application.WorksheetFunction.IsNumber(Target) Then
   Range(Cells(Target.Row, 1), Cells(Target.Row, 50)).Interior.ColorIndex = -4142
   Set rng = Range(Cells(Target.Row, Target.Column).Offset(0, -9), Cells(Target.Row, Target.Column).Offset(0, 9))
   rng.Interior.ColorIndex = 4
End If

End Sub
You could trap for this error, but seeing as how the number would be 1004 (I think) and there are umpteen messages for that number, I don't know if I would. I'd first try to determine if there are 9 cells to the right and 9 to the left of the selected cell. That test would probably be ok if 1004 error was raised since it would be specific to trying to determine that.

The rng object as I've utilized it may not be required for so simple an operation, but I coded for it because I thought there might be more that had to be done with it in terms of testing for 9 left and 9 right. Note: I used 50 as a test. That number is the number of columns in the row that I thought might come into play. You could adjust that number, or test for the last column with data in the clicked row instead.
Thank you sir very much for your time, help and information ! I will use your code and adapt it wisely. Thank you again !
 
Upvote 0
You're welcome and thanks for the acknowledgement.
I just realized that based on the number of cells left and right that need to be considered (9), it is as easy as making sure the target column is at least 10. So this will not error, and if there are not enough cells the row colour is removed. If you don't want the colour to be removed, move the line with -4142 inside the inner IF.
VBA Code:
If Application.WorksheetFunction.IsNumber(Target) Then
   Range(Cells(Target.Row, 1), Cells(Target.Row, 50)).Interior.ColorIndex = -4142
   If Target.Column > 9 Then
      Set rng = Range(Cells(Target.Row, Target.Column).Offset(0, -9), Cells(Target.Row, Target.Column).Offset(0, 9))
      rng.Interior.ColorIndex = 4
   End If
End If
 
Upvote 0
You're welcome and thanks for the acknowledgement.
I just realized that based on the number of cells left and right that need to be considered (9), it is as easy as making sure the target column is at least 10. So this will not error, and if there are not enough cells the row colour is removed. If you don't want the colour to be removed, move the line with -4142 inside the inner IF.
VBA Code:
If Application.WorksheetFunction.IsNumber(Target) Then
   Range(Cells(Target.Row, 1), Cells(Target.Row, 50)).Interior.ColorIndex = -4142
   If Target.Column > 9 Then
      Set rng = Range(Cells(Target.Row, Target.Column).Offset(0, -9), Cells(Target.Row, Target.Column).Offset(0, 9))
      rng.Interior.ColorIndex = 4
   End If
End If
Thank you very much sir ! God Bless You !
 
Upvote 0

Forum statistics

Threads
1,215,020
Messages
6,122,712
Members
449,093
Latest member
Mnur

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top