比较来自两个不同工作表的大量单元格和一列

时间:2019-07-14 08:36:19

标签: excel vba

有两个不同的工作表,我想突出显示sheet1中与sheet2中完全相同的文本值匹配的单元格。

Sheet1是每日生产计划,因此有[名称,订单号,解释]和14天列。

Sheet2是我们提供材料的产品列表。当我用谷歌搜索时,我找不到完全相同的情况。我尝试在下面编写代码,但是没有用。

在Sheet2中,产品列表位于列“ C”中,我有1582个项目。 您的帮助将不胜感激。

Sub Highlights()
    Dim sh1 As Worksheet
    Set sh1 = ThisWorkbook.Sheets("Sheet1")
    Dim sh2 As Worksheet
    Set sh2 = ThisWorkbook.Sheets("Sheet2")
    Dim lastRowNumber As Long, lastColumnNumber As Long

    lastRowNumber = sh1.Range("A1", sh1.Range("A1").End(xlDown)).Rows.Count
    lastColumnNumber = sh1.Range("A1", sh1.Range("A1").End(xlToRight)).Columns.Count

    Dim i As Long, j As Long, x As Long
    For j = 1 To lastRowNumber
        For i = 1 To lastColumnNumber
            For x = 3 To 1584
                If sh1.Cells(j & i) = sh2.Cells(x, 3) Then
                    sh1.Cells(j, i).Select
                    With Selection.Interior
                        .Pattern = xlSolid
                        .PatternColorIndex = xlAutomatic
                        .Color = 49407
                        .TintAndShade = 0
                        .PatternTintAndShade = 0
                    End With
               End If
            Next
        Next
    Next
End Sub

1 个答案:

答案 0 :(得分:0)

如果您只想有条件地遮盖单元格,则可以不循环而完成此操作。

首先,确保两个表都被格式化为表(主页>格式化为表)。在此示例中,我要突出显示的表名为“ t_Stuff”,包含条件的表名为“ t_StuffCriteria”,而包含条件的列名为“颜色”。

  • 转到首页>条件格式>管理规则>添加新规则>使用公式来确定要格式化的单元格
  • 然后添加此公式

(用您自己的信息替换表名和列名:

=COUNTIF(INDIRECT("t_StuffCriteria"),INDIRECT("t_Stuff[@[Colors]]"))>0
  • 单击“格式”,然后选择所需的单元格格式。

    • 单击“确定”>“确定”

    • 在“应用于”框中,突出显示表中应应用突出显示的所有单元格,然后单击“应用”。

enter image description here

enter image description here

这是结果。如果该行中的任何单元格与第二个表的指定列中的任何单元格匹配,则该表的整个行将突出显示。

enter image description here

或者,如果您想严格保留vba ...

Sub ConditionalFormatEntireRow_BasedOnCellMatch()

'~~~> Declare the variables
Dim wsT As Worksheet 'name of the sheet with data to be tested
Dim wsC As Worksheet 'name of the sheet with the criteria
Dim t As ListObject 'table name containing data to be tested
Dim c As ListObject 'table name with the criteria
Dim tCol As Long 'column name with the criteria
Dim f As String 'formula to be used for conditional formatting
Dim fc As FormatCondition
Dim i As Integer

'~~~> Turn off screen updating and alerts.
Application.ScreenUpdating = False
Application.DisplayAlerts = False


'~~~> Inputs.  Modify these with your values.-----------------------------------
'~~~> Store your objects as variables.
Set wsT = Worksheets("Conditional Format")
Set wsC = Worksheets("Conditional Format") 'my example tables are on the same sheet
Set t = wsT.ListObjects("t_Stuff")
Set c = wsC.ListObjects("t_StuffCriteria")
tCol = t.ListColumns("Colors").Index
f = "=COUNTIF(INDIRECT(""" & c.Name & """),INDIRECT(""" & _
    t.Name & "[@[" & t.ListColumns(tCol).Name & "]]""))>0"
'~~~> End of inputs.------------------------------------------------------------

'~~~> Double check the formula variable.
If f = "=COUNTIF(INDIRECT(""t_StuffCriteria""),INDIRECT(""t_Stuff[@[Colors]]""))>0" Then

    Debug.Print "The formula is correct."

End If

With t.Range

    '~~~> Delete any existing formatting conditions.
    .FormatConditions.Delete

    '~~~> Set the format conditions.
    Set fc = .FormatConditions.Add(xlExpression, Formula1:=f)

        '~~~> Specify the formatting that should be applied.
        With fc

            .SetFirstPriority
            .Interior.Color = vbGreen
            .StopIfTrue = False

        End With

End With

'~~~> Turn on screen updating and alerts.
Application.ScreenUpdating = True
Application.DisplayAlerts = True

'~~~> Release the variables from memory.
Set wsT = Nothing
Set wsC = Nothing
Set t = Nothing
Set c = Nothing
tCol = Null
f = vbNullString
Set fc = Nothing

End Sub