[英]Looking to loop through a column in excel and highlight fill each cell if the content matches that of another column
My issue now is with my conditional formatting rules - they are working as expected up to a certain point in column W, which is where the cells should be either highlighted or passed over. 现在我的问题是我的条件格式设置规则-它们按预期工作到W列中的某个点,在该点应突出显示或传递单元格。 I have included a set of data from column W and column Z (the reference data) where there are matches that are not being highlighted. 我包括了W列和Z列中的一组数据(参考数据),其中有未突出显示的匹配项。 In the Column W data I have bolded the numbers that should be highlighted. 在W列数据中,我将应突出显示的数字加粗了。
Column Z - Z506-Z550 Z栏-Z506-Z550
Column W - W1144-W1155 W栏-W1144-W1155
The conditional formatting formula I have is =VLOOKUP($W4,$Z4:$Z922,1,FALSE) 'Applies To' =$W$4:$W$3600 我拥有的条件格式公式为= VLOOKUP($ W4,$ Z4:$ Z922,1,FALSE)'适用于'= $ W $ 4:$ W $ 3600
I am not sure what is causing this conditional formatting to fail here. 我不确定是什么导致这种条件格式在这里失败。
I am trying to automate a manual process of cross referencing data and highlighting a cell if the contents are found anywhere in another column of data. 我正在尝试自动进行交叉引用数据并突出显示单元格的手动过程(如果在另一列数据中的任何位置都可以找到内容)。 However, the amount of data in both of these columns is not the same. 但是,这两个列中的数据量都不相同。 And unfortunately, the column I need to loop through and check each cell often has either blank cells or cells that are dashed ("---"). 不幸的是,我需要遍历并检查每个单元格的列通常具有空白单元格或带虚线的单元格(“ ---”)。
I started with conditional formatting but it was not working properly so I am now on VBA. 我从条件格式开始,但是它不能正常工作,所以现在使用VBA。
Private Sub Workbook_Open()
Dim LastRow As Long
Range("W4").Select
LastRow = Range("W4").End(xlDown).Row
Do Until ActiveCell.Row = LastRow
If Not IsEmpty(Application.Match(ActiveCell.Value, Range("Z:Z"), 0))
Then
ActiveCell.Interior.Color = vbGreen
ActiveCell.Offset(1, 0).Select
End If
Loop
End Sub
Right now the code has a couple issues. 现在,代码有几个问题。 It is not finding the last row correctly - when debugging it shows as 65, but should be 3,535 in the test case I'm using. 它找不到正确的最后一行-调试时显示为65,但在我使用的测试用例中应为3,535。 Additionally, my match statement is not working, as it is highlighting every cell instead of only those whose content is found in column Z. And, it highlights up to row 410, which means my Do Until loop must be wrong as well. 此外,我的match语句不起作用,因为它突出显示了每个单元格,而不是仅突出显示了在Z列中找到的单元格。而且,它突出显示了第410行,这意味着我的Do Before循环也必须是错误的。
After figuring out the one column I eventually need to allow checking columns AA and AB for content matches. 在找出一列之后,我最终需要允许检查AA和AB列中的内容是否匹配。
Thanks! 谢谢!
As mentioned in the comment, conditional formatting is the way to go. 如评论中所述,条件格式是解决之道。 I just tried the following as a conditional format. 我只是尝试以下作为条件格式。
=VLOOKUP($A2,$C$2:$C$7,1,FALSE)
A column of numbers in A as the numbers to be cross-referenced, and a list in column C which are the numbers to be checked. A中的一列数字作为要交叉引用的数字,C列中的列表是要检查的数字。 It works perfectly. 它运作完美。
I recommend to use Conditional Formatting. 我建议使用条件格式。 The following is just to explain what was wrong with your code: 以下只是说明您的代码出了什么问题:
End(xlDown)
will find the next free cell (not the last used). 使用End(xlDown)
将找到下一个空闲单元(而不是最后使用的单元)。 Instead use End(xlUp)
from the very last cell of the worksheet. 而是从工作表的最后一个单元格使用End(xlUp)
。 Application.Match
does not return a cell but a row number. Application.Match
不返回单元格,而是返回行号。 Therefore IsEmpty
does not work. 因此, IsEmpty
不起作用。 In the end something like this should work: 最后,这样的事情应该起作用:
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("SheetName")
Dim LastRow As Long
LastRow = ws.Cells(ws.Rows.Count, "W").End(xlUp).Row
Dim MatchRow As Variant
Dim iRow As Long
For iRow = 4 To LastRow
MatchRow = 0 'initialize
MatchRow = Application.Match(ws.Cells(iRow, "W").Value, ws.Range("Z:Z"), 0)
If MatchRow > 0 Then
ws.Cells(iRow, "W").Interior.Color = vbGreen
End If
Next iRow
Note that Contidional Formatting would be a much better solution. 请注意,按条件格式化将是更好的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.