简体   繁体   English

如何比较两个工作表的列,突出显示相同的值#code

[英]How to Compare two worksheets' columns, highlight the same values #code

I am a newbie here, also a newbie in VBA. 我是这里的新手,还是VBA中的新手。

I have two worksheets that I wanted to compare. 我有两个要比较的工作表。

Let's say, 比方说

  • Sheet1 工作表1
    • Column DWG. 列DWG。 NO 没有
    • Column SYM 列SYM
  • Sheet2 工作表2
    • Column DWG. 列DWG。 NO 没有
    • Column SYM 列SYM

But the column position of DWG and SYM in sheet2 is not always the same so first I need to locate the positions of the column before comparing. 但是DWG和SYM在sheet2中的列位置并不总是相同,因此在比较之前,我首先需要找到列的位置。 Then highlight the comparison. 然后突出显示比较。

Take note also that the sheets have thousands of row data and multiple columns. 还请注意,工作表具有数千行数据和多列。 But only two columns are needed to be compared. 但是只需要比较两列。

This is the working code: 这是工作代码:

Sub LookForMatches()
    Dim rng1 As Range, rng2 As Range, c1 As Range, c2 As Range
    Dim rng3 As Range, rng4 As Range, c3 As Range, c4 As Range
'set ranges
    Set rng1 = Sheets("datax").Range("C5", Sheets("datax").Range("C" & Rows.Count).End(xlUp))
    Set rng2 = Sheets("datay").Range("AC4", Sheets("datay").Range("AC" & Rows.Count).End(xlUp))
    Set rng3 = Sheets("datax").Range("F5", Sheets("datax").Range("F" & Rows.Count).End(xlUp))
    Set rng4 = Sheets("datay").Range("AH4", Sheets("datay").Range("AH" & Rows.Count).End(xlUp))

'reset colour
    rng1.Interior.Color = 16777215
    rng2.Interior.Color = 16777215
    rng3.Interior.Color = 16777215
    rng4.Interior.Color = 16777215
'loop values in range
    For Each c1 In rng1
        If Not c1.Interior.ColorIndex = 16777215 And c1 <> "" And c1 <> 0 Then
            For Each c2 In rng2
                If c1 = c2 And c2.Address <> c1.Address Then
                    c1.Interior.Color = RGB(255, 255, 0)
                    c2.Interior.Color = RGB(255, 255, 0)
                End If
            Next c2
        End If
    Next c1
'loop values in next range
    For Each c3 In rng3
        If Not c3.Interior.ColorIndex = 16777215 And c3 <> "" And c3 <> 0 Then
            For Each c4 In rng4
                If c3 = c4 And c4.Address <> c3.Address Then
                    c3.Interior.Color = RGB(255, 255, 0)
                    c4.Interior.Color = RGB(255, 255, 0)
                End If
            Next c4
        End If
    Next c3
    MsgBox ("Checking Done")
    Application.Goto Sheets("datay").Range("AA1"), True
End Sub

But sheet2's column location is defined. 但是sheet2的列位置已定义。 However, it should not be defined based on the column number but on the header name because the position of column is varying. 但是,由于列的位置是变化的,因此不应基于列号而是基于标题名称来定义它。

Thank you so much. 非常感谢。

I don't understand why you are doing this using VBA: I created two columns, A:A and B:B, and I used conditional formatting to colour cells, based on their presency in column A:A, based on this formula: 我不明白您为什么使用VBA进行此操作:我创建了两列A:A和B:B,并且根据以下公式,根据它们在A:A列中的存在情况对单元格使用了条件格式设置:

=IFNA(MATCH(B2;$A$2:$A$4;0);FALSE)      // for colouring if found
=NOT(IFNA(MATCH(B2;$A$2:$A$4;0);FALSE)) // for colouring otherwise if not found

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

相关问题 比较两个工作表和突出显示差异 - Compare Two Worksheets and Highlight Difference 比较同一文件的两个工作表中的数据并突出显示单元格 - Compare data in two worksheets of same file and highlight the cell 比较不同工作表的两列,并突出显示两列中的共同值 - Comparing two columns of different worksheets and highlight common values in both columns 比较同一工作簿中两个Excel工作表中的两组列 - compare two set of columns in two excel worksheets in same workbook 如何突出显示来自两个不同范围和工作表的匹配值? - How to highlight matched values from two different ranges and worksheets? 比较两个工作表并将匹配项和唯一值粘贴到同一工作簿的不同工作表中 - Compare two worksheets and paste matches and unique values into separate worksheets of the same workbook 如何比较同一工作簿但不同工作表中的两个命名范围? - How to compare two named ranges in same workbook but different worksheets? 如何比较两列之间的字符并突出显示使用宏相同的特定单元格 - How to compare the characters between two columns and highlight particular cell which are same using macro 比较 excel &amp; Highlight 中的两列 - Compare two columns in excel & Highlight 比较不同工作表之间的 2 列,然后粘贴值 - Compare 2 columns between different worksheets then paste values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM