Vba - Excel Formula Cell Based On Background Color - Stack Overflow

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

27/11/2020 vba - Excel Formula Cell Based on Background color - Stack Overflow

Excel Formula Cell Based on Background color


Asked 3 years, 9 months ago Active 9 months ago Viewed 22k times

I need a formula in EXCEL that place a number 1 in the cell next to the cell where the cell background is RED. See example below.

Is this possible at all without VBA?

vba excel excel-formula formulas

edited Mar 6 '18 at 8:59 asked Feb 2 '17 at 9:38


CallumDA Etienne
11.3k 6 25 45 6,371 40 99 154

Not possible without VBA. Very simple with VBA... your choice – CallumDA Feb 2 '17 at 9:52

Can you give me the answer in VBA then and where to place the code? – Etienne Feb 2 '17 at 9:52

3 Answers Active Oldest Votes

Open the VBA editor and add a new module. Do this by going to the Developer tab and clicking Visual Basic . If you don't have the
developer tab on the ribbon you will need to add it (do a quick Google search). Once the VBA editor is open, right click on the VBA
By using our site,
has you
youracknowledge that you
onhave
the read and insert
understand our Cookie Policy, Privacy Policy, and our Terms of Service.
4 project which workbook name left and a module.

https://stackoverflow.com/questions/41998676/excel-formula-cell-based-on-background-color 1/4
27/11/2020 vba - Excel Formula Cell Based on Background color - Stack Overflow

Place the following code into the new module:

Function IsRed(rng As Range) As Integer


IsRed = (rng.Interior.Color = vbRed) * -1
End Function

then you can use the formula =IsRed(A1) to determine if A1 has a red background

note: this uses the default red in the standard colours

answered Feb 2 '17 at 9:56


CallumDA
11.3k 6 25 45

Thank you very much! Yes very easy! – Etienne Feb 2 '17 at 11:57

This can be done from Name Manager this can be accessed by pressing Ctrl + F3 .

6 You will want to create a named reference (i called this "color") and have it refer to =GET.CELL(63,OFFSET(INDIRECT("RC",FALSE),0,-1)) in
the formula bar.

Now you can use this 1 cell to the right to determine the color index number of a cell:

By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service.

https://stackoverflow.com/questions/41998676/excel-formula-cell-based-on-background-color 2/4
27/11/2020 vba - Excel Formula Cell Based on Background color - Stack Overflow

So as red is color index 3 in the cell next to it you can apply the formula:

=IF(color=3,1,0)

answered Feb 2 '17 at 10:11


Glitch_Doctor
2,944 2 13 29

You can achieve it manually without VBA using an autofilter:

4 1. Make sure you have a title above the column with colours and above the column where you want the value 1 placed
2. Add an Autofilter (Select both columns, click the Filter button on the Data tab of the ribbon)

3. Click the drop down filter on the column with colours, then click on Filter by Colour, the choose the Red colour
4. In your second column, enter a 1 in every visible cell. (Enter 1 in the first cell, then fill down. Or, select all cells, type 1 then press
ctrl-enter)

By using our site, you acknowledge that you have read and understand our Cookieedited
Policy , Privacy
Feb 2 '17 atPolicy
10:27 , and our Terms of Service
answered Feb 2 .'17 at 10:15

https://stackoverflow.com/questions/41998676/excel-formula-cell-based-on-background-color 3/4
27/11/2020 vba - Excel Formula Cell Based on Background color - Stack Overflow

Michael
3,883 2 8 23

By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service.

https://stackoverflow.com/questions/41998676/excel-formula-cell-based-on-background-color 4/4

You might also like