[英]Comparing huge range of cells and one column from two different worksheets
有兩個不同的工作表,我想突出顯示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
如果您只是希望有條件地遮蓋單元格,則可以不循環而完成此操作。
首先,確保兩個表都被格式化為表(主頁>格式化為表)。 在此示例中,我要突出顯示的表名為“ t_Stuff”,包含條件的表名為“ t_StuffCriteria”,而包含條件的列名為“顏色”。
(將表名和列名替換為您自己的信息:
=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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.