简体   繁体   English

如果内容与另一列相匹配,则希望在excel中循环浏览一列并突出显示填充每个单元格

[英]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

  • 233892 233892
  • 233899 233899
  • 959460 959460
  • 156311 156311
  • 515114 515114
  • 549794 549794
  • 562793 562793
  • 372953 372953
  • 230659 230659
  • 230717 230717
  • 2051205586 2051205586
  • 364834 364834
  • 790760 790760
  • 334588 334588
  • 538149 538149
  • 288261 2882​​61
  • 19326 19326
  • 267428 267428
  • Net 90 净90
  • 473853 473853
  • 3211221994 3211221994
  • 264556 264556
  • 260798 260798
  • 156271 156271
  • 509597 509597
  • 2211211506 2211211506
  • 800990 800990
  • 597593 597593
  • 431759 431759
  • 377289 377289
  • 224118 224118
  • 178966 178966
  • 276840 276840
  • 430269 430269
  • 431923 431923
  • 431986 431986
  • 547439 547439
  • 512399 512399
  • 234975 234975
  • 512203 512203
  • 602547 602547
  • 443537 443537
  • 376759 376759
  • 284287 284287
  • 608745 608745

Column W - W1144-W1155 W栏-W1144-W1155

  • 233892 233892
  • 367164 367164
  • 368384 368384
  • 344813 344813
  • 233899 233899
  • 233899 233899
  • 233895 233895
  • - -
  • 233917 233917
  • 284287 284287
  • 376759 376759
  • 443537 443537

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. 我不确定是什么导致这种条件格式在这里失败。

Ignore below - now working with conditional formatting instead of vba 在下面忽略-现在使用条件格式而不是vba

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: 以下只是说明您的代码出了什么问题:

  1. Always define which worksheet you mean and avoid using Select in Excel VBA . 始终定义您要表示的工作表,并避免在Excel VBA中使用“选择”
  2. Using 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)
  3. 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.

相关问题 从列中选择每个单元格,并在另一个工作簿中的列中循环(如果存在)Excel VBA宏 - select each cell from a column and loop through a column in another workbook if it exists Excel VBA Macro 对于一列中的每个单元格,检查下一列中的多个匹配项,然后突出显示它们 - For each cell in a column, check multiple matches in the next column and then highlight them 当 Excel 中的所有内容都包含在另一列中时,如何突出显示该单元格 - How do I highlight a cell in Excel when all its content is contained in another column 比较一列中的excel单元对以查找另一列中的匹配项或差异 - Comparing excel cell pairs in a column for matches or differences in another column Excel VBA - 循环插入列中的复选框,如果选中则填充另一列 - Excel VBA - Loop through checkboxes inserted in a column and fill another column if checked VBA 循环 excel 中 A 列中的每个单元格 - VBA To loop each cell in the Column A in excel 我如何遍历一列,如果单元格符合某些条件,则将整行粘贴到另一个工作表中 - How do I loop through a column, if a cell matches some criteria then paste that whole row into another worksheet Excel-如果在另一列中找到内容,则格式化单元格 - Excel - Format cell if content found in another column EXCEL VBA - 遍历列中的单元格,如果不为空,则将单元格值打印到另一列中 - EXCEL VBA - Loop through cells in a column, if not empty, print cell value into another column 如果Excel中包含的单词位于另一列中,如何在Excel中突出显示单元格 - How to highlight a cell in Excel if the word it contains is in another column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM