简体   繁体   English

如何根据 VBA 中的另一个单元格为其他单元格着色

[英]How to color other cells based on another cell in VBA

My knowledge on VBA is limited, so I struggle even with this simple task.我对 VBA 的了解有限,所以即使完成这个简单的任务我也很挣扎。 I want to check every cell in column B, if that cell value is duplicated in an another worksheet's column A. Then if it has duplicate values in that range, it colors the cell to yellow.我想检查 B 列中的每个单元格,如果该单元格值在另一个工作表的 A 列中重复。然后,如果它在该范围内具有重复值,则 colors 单元格变为黄色。 Ive got this part working.我已经让这部分工作了。 The next thing I want to do is to color the cells in that same row in columns C, D and F. This part is not working for me, it gives me an Type Mismatch error.接下来我要做的是为 C、D 和 F 列中同一行中的单元格着色。这部分对我不起作用,它给了我一个类型不匹配错误。 Any help on this would be appreciated.对此的任何帮助将不胜感激。

Sub Mark_Duplicates()

    Dim Cell As Variant
    Dim Source As Range
    Dim Source2 As Range
    Dim rownumber As Variant

    Set Source = Range("B1:B1000")
    Set Source2 = Worksheets("Chain").Range("A1:A1000")

    For Each Cell In Source

        If Application.WorksheetFunction.CountIf(Source2, Cell) > 1 Then

            rownumber = ActiveCell.Row
            Cell.Interior.Color = RGB(255, 255, 0)
            Range("C" & rownumber).Interior.Color = RGB(255, 255, 0)
            Range("D" & rownumber).Interior.Color = RGB(255, 255, 0)
            Range("F" & rownumber).Interior.Color = RGB(255, 255, 0)

        End If

    Next

End Sub

As per @BigBen, the most obvious thing to correct in your code is the way you concatenate a range.根据@BigBen,在您的代码中要纠正的最明显的事情是您连接范围的方式。 You have currently used the arithmetic addition ( + ) whereas the correct VBA syntax would be the & .您当前使用了算术加法 ( + ),而正确的 VBA 语法是&

With that out of the way, you also shouldn't refer to the ActiveCell .除此之外,您也不应该引用ActiveCell Since you never activate any other cell, the referenced row will always stay the same.由于您从未激活任何其他单元格,因此引用的行将始终保持不变。 In general, as per my comment, to refer to something active is bad practice .一般来说,根据我的评论,引用一些活跃的东西是不好的做法 Instead use Cell.Row .而是使用Cell.Row

Another good practice would be to use Long data type variables for full integer numbers instead of data type Variant .另一个好的做法是对完整的 integer 数字使用Long数据类型变量,而不是数据类型Variant Amongst the disadvantages to leave it up to VBA to decide on a variant data type, is that using Long takes 4 bytes, whereas a variant using numbers takes 22 .将它留给 VBA 来决定变体数据类型的缺点之一是使用Long需要4 个字节,而使用数字的变体需要 22 So, you are slowing down your code.所以,你正在减慢你的代码。 In fact, you won't need it per se.事实上,你本身并不需要它。

You can also, instead of coloring cell by cell, color a range at once.您也可以一次为一个范围着色,而不是逐个单元格地着色。 You can simply refer to a larger range.您可以简单地参考更大的范围。

A last note is that you never fully qualified, at the very least, the referenced sheet for Source , which in that case will refer to B1:B1000 on again, the ActiveSheet .最后一点是,您至少从未完全限定Source的引用表,在这种情况下,它将再次引用B1:B1000 ,即ActiveSheet I personally prefer to refer to a sheets CodeName .我个人更喜欢参考工作表CodeName

To conclude, you could re-write your code like:总而言之,您可以重新编写代码,例如:

Sub Mark_Duplicates()

Dim Source1 As Range: Set Source1 = Sheet1.Range("B1:B1000") 'Use a worksheet's CodeName instead of sheet name (which can change)
Dim Source2 As Range: Set Source2 = Sheet2.Range("A1:A1000")
Dim cell As Range

For Each cell In Source1
    If Application.CountIf(Source2, cell) > 1 Then
        Sheet1.Range(Replace("B?:D?,F?", "?", cell.Row)).Interior.Color = RGB(255, 255, 0)
    End If
Next

End Sub

If your range would grow to a larger dataset, it's wise to no longer iterate over a Range but instead switch to an array which makes for much faster processing.如果您的范围将增长到更大的数据集,明智的做法是不再遍历范围,而是切换到数组,这样可以加快处理速度。

Question remains, do you even need VBA at all (as per @BigBen), as you could do this through conditional formatting (the larger the range, the slower your project as CF is volatile per definition)问题仍然存在,您是否甚至需要 VBA (根据@BigBen),因为您可以通过条件格式来做到这一点(范围越大,您的项目越慢,因为 CF 每个定义都不稳定)

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

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