EXCEL:突出显示在同一列中重新分配数据

我在Excel中有一列(D)的数据已经使用: = TEXT(B2,“###”)sorting。 这是显示一个数据列表(数字),有一个额外的“REP 1”反对它。

并非所有数据都有“REP 1”,所以我想突出显示所有包含数字和“REP 1”的字段。 我可以突出显示所有的“REP 1”字段,看看它是否有重复,但这只是一个样本表。 我有8000多个领域需要经过,而且太费时了。

请看以下链接的例子:

所需的格式

我希望这一切都有道理。 谢谢,

蒂姆。

不知道是否可以使用条件格式,但这个VBA代码应该工作。 您的数据不必按任何特定的顺序sorting,并假设您正在格式化的数据是在D列。我已经testing了几百行,它工作正常,所以应该罚款大数据集。 我试着通过代码中的注释来解释代码的作用。

Sub formatCells() Dim x As Variant Dim y As Variant Dim searchval As String Dim a As Variant Dim lastrow As Long Dim rng As Range Application.ScreenUpdating = False ' turn off screen updates lastrow = Cells(Rows.Count, 4).End(xlUp).Row 'find the last blank cell x = 2 'set rownumber y = 4 'set columnnumber While Cells(x, y) <> "" ' create loop If InStr(Cells(x, y), "REP1") Then 'search for string in cell Cells(x, y).Interior.Color = RGB(255, 0, 0) 'if string exists fill cell End If x = x + 1 ' loop Wend ' end loop x = 2 ' reset row number y = 4 ' reset column number While Cells(x, y) <> "" ' create loop 2 If Cells(x, y).Interior.Color = RGB(255, 0, 0) And InStr(Cells(x, y), "REP1") Then 'if cells is red and contains Rep1 a = Cells(x, y).Value ' set a to equal the cell that is red and and contains REP1 searchval = Left(a, Len(a) - 5) 'remove space and REP1 and set value ready for search If searchval <> "" Then 'if theres a search value available run steps below With Range("D1:D" & lastrow) 'set range to be column A Set rng = .Find(What:=searchval, _ After:=.Cells(1), _ LookIn:=xlValues, _ LookAt:=xlWhole, _ SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious, _ MatchCase:=False) If Not rng Is Nothing Then 'If search value is found Application.Goto rng, True ' go to cell ActiveCell.Interior.Color = RGB(255, 0, 0) 'set cell to red End If End With End If End If x = x + 1 'loop 2 Wend ' end loop 2 End Sub 

编辑 – 看B列不是D

  Sub formatCells() Dim x As Variant Dim y As Variant Dim searchval As String Dim a As Variant Dim lastrow As Long Dim rng As Range Application.ScreenUpdating = False ' turn off screen updates lastrow = Cells(Rows.Count, 2).End(xlUp).Row 'find the last blank cell x = 2 'set rownumber y = 2 'set columnnumber While Cells(x, y) <> "" ' create loop If InStr(Cells(x, y), "REP1") Then 'search for string in cell Cells(x, y).Interior.Color = RGB(255, 0, 0) 'if string exists fill cell End If x = x + 1 ' loop Wend ' end loop x = 2 ' reset row number y = 2 ' reset column number While Cells(x, y) <> "" ' create loop 2 If Cells(x, y).Interior.Color = RGB(255, 0, 0) And InStr(Cells(x, y), "REP1") Then 'if cells is red and contains Rep1 a = Cells(x, y).Value ' set a to equal the cell that is red and and contains REP1 searchval = Left(a, Len(a) - 5) 'remove space and REP1 and set value ready for search If searchval <> "" Then 'if theres a search value available run steps below With Range("B1:B" & lastrow) 'set range to be column A Set rng = .Find(What:=searchval, _ After:=.Cells(1), _ LookIn:=xlValues, _ LookAt:=xlWhole, _ SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious, _ MatchCase:=False) If Not rng Is Nothing Then 'If search value is found Application.Goto rng, True ' go to cell ActiveCell.Interior.Color = RGB(255, 0, 0) 'set cell to red End If End With End If End If x = x + 1 'loop 2 Wend ' end loop 2 End Sub