【问题标题】:Comparing huge range of cells and one column from two different worksheets比较来自两个不同工作表的大量单元格和一列
【发布时间】:2019-07-14 08:36:19
【问题描述】:

有两个不同的工作表,我想突出显示 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

【问题讨论】:

  • 您的代码不起作用的主要原因是sh1.Cells(j & i) 应该是sh1.Cells(j, i)。也就是说,对于任何大小的数据集,这将是非常缓慢的。考虑转换为变体数组方法。 SO上有很多例子

标签: excel vba


【解决方案1】:

如果您只是希望有条件地对单元格进行着色,则无需循环即可完成此操作。

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

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

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

=COUNTIF(INDIRECT("t_StuffCriteria"),INDIRECT("t_Stuff[@[Colors]]"))>0
  • 单击“格式”并选择您希望单元格的格式。

    • 点击确定 > 确定

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

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

或者,如果你想严格保持 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多