简体   繁体   English

如何比较多个命名列范围中相应单元格的值并更改另一个相应单元格中的内部颜色

[英]How to compare values of corresponding cells in multiple named column ranges and change interior color in another corresponding cell

I want to write a code in Excel VBA that compares the values in corresponding cells in 3 named ranges (say, "Peter", "Paul" & "John"), and if all three values are >= 3, then the interior color of the corresponding cell in a fourth named range say, "James", is changed to Green.我想在 Excel VBA 中编写一个代码,比较 3 个命名范围(例如“Peter”、“Paul”和“John”)中相应单元格中的值,如果所有三个值都 >= 3,则内部颜色在第四个命名范围内的相应单元格中,例如“James”,更改为绿色。 I wrote the code using offset but the code doesn't work properly if I insert new columns in-between "Peter", "Paul" and "John".我使用偏移量编写了代码,但如果我在“Peter”、“Paul”和“John”之间插入新列,代码将无法正常工作。 Please can you help me write a code that uses named ranges, to avoid an error when new columns are added?请您帮我编写一个使用命名范围的代码,以避免在添加新列时出错? Thank you.谢谢你。

If Cell.Value >= 3 Then
            If Cell.Offset(0, 1).Value >= 3 Then
                If Cell.Offset(0, 2).Value >= 3 Then
                    If Cell.Offset(0, 3).Value >= 3 Then
                        Cell.Offset(0, 4).Interior.ColorIndex = 4
                    End If
                End If
            End If
        End If
    Next Cell

This works这有效

Dim xCel As Range, rPtr As Long
Dim xSht As Worksheet

Set xSht = ActiveSheet
rPtr = 0
For Each xCel In xSht.Range("Peter").Cells
   rPtr = rPtr + 1
   If xCel.Value >= 3 Then
      If xSht.Range("Paul").Cells(rPtr, 1).Value >= 3 Then
         If xSht.Range("John").Cells(rPtr, 1).Value >= 3 Then
            xSht.Range("James").Cells(rPtr, 1).Interior.ColorIndex = 34
         End If
      End If
   End If
Next xCel

This solves the problem you mentioned, but it still presumes all ranges are the same length and single column这解决了你提到的问题,但它仍然假定所有范围都是相同的长度和单列

暂无
暂无

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

相关问题 EXCEL:如何对与另一个“合并”单元格相对应的许多单元格/值求和? - EXCEL: How to sum many cells/ values corresponding to another “merged” cell? 如何匹配多个列值并在Excel中提取相应的单元格值 - How match multiple column values and extract corresponding cell values in Excel 使用宏根据另一个工作表中的相应单元格值突出显示工作表中的单元格 - Highlight cells in a sheet based on corresponding cell values in another sheet with a macro 指示具有多个对应值的单元格 - Indicating cells with multiple corresponding values 如何使用XlsxWriter根据另一个工作表中的相应单元格值来格式化一个工作表中的所有单元格? - How to format all cells in one sheet based on corresponding cell values in another sheet using XlsxWriter? 在Excel中,如何将单元格与另一张工作表中的列进行比较,以及如何将匹配的实例用于来自相应行的值的总和 - In Excel, how to compare a cell to a column in another sheet and use the matched instances for sum of a value from the corresponding rows 当更新对应行中的另一个单元格时,如何在固定列而不是动态行中获取一个单元格? - How can I get a cell in a fixed column, but dynamic row, to change when another cell in the corresponding row is updated? 返回另一张工作表中相应单元格的单元格 - Return cell of corresponding cells in another sheet 将一列中所有单元格的值添加到另一列中的相应单元格,然后清除原始单元格 - Add value in all cells within a column to corresponding cell in another column and then clear original cells 单元格的内部颜色,直到列中包含数据的最后一个单元格 - Interior color of cells up to the last cell with data in a column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM