2016-07-15 63 views
0

enter image description here我有今天的數據在D列,我想與昨天的F列數據進行比較,明智的行。VBA StrComp - 比較值與例外

下面是我用來比較和突出顯示重複項的代碼。

A)突出顯示我不想要的空白單元格。 B)我想處理一些例外就像我不希望強調$ 0.00或將在清算後的顏色突出顯示宏運行多次特定文本「無數據」

Sub CompareAndHighlight() 

Dim Myrng1 As Range, Myrng2 As Range, i As Long, j As Long 

Application.ScreenUpdating = False 

For i = 3 To Sheets("Sheet1").Range("D" & Rows.Count).End(xlUp).Row 
     Set Myrng1 = Sheets("Sheet1").Range("D" & i) 
    For j = 3 To Sheets("Sheet1").Range("F" & Rows.Count).End(xlUp).Row 
     Set Myrng2 = Sheets("Sheet1").Range("F" & j) 
     If StrComp(Trim(Myrng1.Text), Trim(Myrng2.Text), vbTextCompare) = 0 Then 
     'If Myrng1.Value = Myrng2.Value Then 
     Myrng1.Interior.Color = RGB(255, 255, 0) 
     End If 
     Set Myrng2 = Nothing 
    Next j 
    Set Myrng1 = Nothing 
Next i 

Application.ScreenUpdating = True 

End Sub 

Data giving random errors

+0

你能寫出更具體什麼是你想實現什麼?看起來,根據我所看到/理解的情況,如果這對你很有用,可以通過條件格式很容易地完成,而不需要任何VBA。 –

+0

對不起@JohnBus​​tos我只看到您的評論。 – Lowpar

回答

0

使用條件格式功能。

Columns("A:A").Select 
Selection.FormatConditions.AddUniqueValues 
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority 
Selection.FormatConditions(1).DupeUnique = xlDuplicate 
With Selection.FormatConditions(1).Font 
    .Color = -16383844 
    .TintAndShade = 0 
End With 
With Selection.FormatConditions(1).Interior 
    .PatternColorIndex = xlAutomatic 
    .Color = 13551615 
    .TintAndShade = 0 
End With 
Selection.FormatConditions(1).StopIfTrue = False 

然後在這之後創建一個循環,通過你的範圍去並打開了單元格的顏色沒有色您的條件得到滿足,或者你可以只篩選數據以排除你的情況下,如「無數據「,並將結果複製並粘貼到新列中。事實上,你並不需要vba。

0

與VBA堅持,你可以試試下面的代碼:

Option Explicit 

Sub CompareAndHighlight() 
    Dim refRng As Range, cell As Range 

    Application.ScreenUpdating = False 

    With Worksheets("Sheet1") 
     Set refRng = .Range("F3", .Cells(.Rows.Count, "F").End(xlUp)).SpecialCells(XlCellType.xlCellTypeConstants) 
     For Each cell In .Range("D3", .Cells(.Rows.Count, "D").End(xlUp)).SpecialCells(XlCellType.xlCellTypeConstants) 
      If cell.value <> 0 And cell.value <> "No Data" Then 
       If refRng.Find(what:=cell.value, LookIn:=xlFormulas, lookat:=xlWhole, MatchCase:=False) Is Nothing Then cell.Interior.color = RGB(255, 255, 0) 
      End If 
     Next cell 
    End With 

    Application.ScreenUpdating = True 
End Sub 
+0

正在工作,當我用 - 如果refRng.Find(what:= cell.value,LookIn:= xlFormulas,lookat:= xlWhole,MatchCase:= False)是Nothing Then cell.Interior.color = RGB(255,255,0 )[絕不如] – sumit

+0

很高興能有所幫助。 Tgen,請將我的回答標記爲已接受。謝謝 – user3598756

+0

謝謝user3598756,但是我仍然面臨一個問題,宏是隨機突出顯示非重複的項目。 – sumit