简体   繁体   English

基于背景颜色的 Excel 公式单元格

[英]Excel Formula Cell Based on Background color

I need a formula in EXCEL that place a number 1 in the cell next to the cell where the cell background is RED.我需要一个 EXCEL 公式,在单元格背景为红色的单元格旁边的单元格中放置一个数字 1。 See example below.请参阅下面的示例。

在此处输入图片说明

Is this possible at all without VBA?如果没有 VBA,这可能吗?

This can be done from Name Manager this can be accessed by pressing Ctrl + F3 .这可以从Name Manager完成,这可以通过按Ctrl + F3访问。

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.您将要创建一个命名引用(我称之为“颜色”)并让它在公式栏中引用=GET.CELL(63,OFFSET(INDIRECT("RC",FALSE),0,-1))

Now you can use this 1 cell to the right to determine the color index number of a cell:现在您可以使用右侧的 1 个单元格来确定单元格的颜色索引号:

例子

So as red is color index 3 in the cell next to it you can apply the formula:因此,由于红色是旁边单元格中的颜色索引 3,您可以应用以下公式:

=IF(color=3,1,0)

Open the VBA editor and add a new module.打开 VBA 编辑器并添加一个新模块。 Do this by going to the Developer tab and clicking Visual Basic .为此,请转到“ Developer选项卡并单击“ Visual Basic If you don't have the developer tab on the ribbon you will need to add it (do a quick Google search).如果功能区上没有开发人员选项卡,则需要添加它(在 Google 上进行快速搜索)。 Once the VBA editor is open, right click on the VBA project which has your workbook name on the left and insert a module.打开 VBA 编辑器后,右键单击左侧有您的工作簿名称的 VBA 项目并插入一个模块。

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那么你可以使用公式=IsRed(A1)来确定A1是否有红色背景

note: this uses the default red in the standard colours注意:这使用标准颜色中的默认红色

You can achieve it manually without VBA using an autofilter:您可以使用自动过滤器在没有 VBA 的情况下手动实现它:

  1. Make sure you have a title above the column with colours and above the column where you want the value 1 placed确保在带有颜色的列上方和要放置值 1 的列上方有标题

  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.在第二列中,在每个可见单元格中输入 1。 (Enter 1 in the first cell, then fill down. Or, select all cells, type 1 then press ctrl-enter) (在第一个单元格中输入 1,然后向下填充。或者,选择所有单元格,输入 1 然后按 ctrl-enter)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM