I have created one VBA Which will compare the two sheets of same excel file. If the data in Sheet A is not accurate it will change the color of that row into red, also i have applied filter if my color gets changed.
Now the problem is It is not working in appropriate manner. Like if my data is same then also it is applying filter.
See my code below
Sub Validate_Metadata()
Dim myRng As Range
Dim lastCell As Long
Dim flag As Boolean
'Get the last row
Dim lastRow As Integer
lastRow = ActiveSheet.UsedRange.Rows.Count
'Debug.Print "Last Row is " & lastRow
Dim c As Range
Dim d As Range
Application.ScreenUpdating = False
For Each c In Worksheets("Sheet1").Range("A2:A" & lastRow).Cells
For Each d In Worksheets("Sheet2").Range("A2:A" & lastRow).Cells
c.Interior.Color = vbRed
flag = False
If (InStr(1, d, c, 1) > 0) Then
c.Interior.Color = vbWhite
Exit For
End If
Next
Next
If (flag <> True) Then
ActiveSheet.Range("A1:A" & lastRow).AutoFilter Field:=1, Criteria1:=RGB(255, 0 _
, 0), Operator:=xlFilterCellColor
End If
Application.ScreenUpdating = True
End Sub
Thanks
Try this:
Sub Validate_Metadata()
Dim myRng As Range
Dim lastCell As Long
Dim flag As Boolean
'Get the last row
Dim lastRow As Integer
Dim localFlag As Boolean
lastRow = ActiveSheet.UsedRange.Rows.Count
'Debug.Print "Last Row is " & lastRow
Dim c As Range
Dim d As Range
Application.ScreenUpdating = False
flag = True
For Each c In Worksheets("Sheet1").Range("A2:A" & lastRow).Cells
localFlag = False
For Each d In Worksheets("Sheet2").Range("A2:A" & lastRow).Cells
c.Interior.Color = vbRed
If (InStr(1, d, c, 1) > 0) Then
c.Interior.Color = vbWhite
localFlag = True
Exit For
End If
Next
flag = flag And localFlag
Next
If (flag <> True) Then
ActiveSheet.Range("A1:A" & lastRow).AutoFilter Field:=1,
Criteria1:=RGB(255, 0 _
, 0), Operator:=xlFilterCellColor
End If
Application.ScreenUpdating = True
End Sub
You are first changing the interior color of the cell to red and then checking for condition. If it matches you are then again changing cell color to white . I guess this is not a good approach. Instead first check the condition and then change the color only when there is no match.
Something like this:
Sub Validate_Metadata()
Dim myRng As Range
Dim lastCell As Long
Dim flag As Boolean, found As Boolean 'new boolean variable declared
'Get the last row
Dim lastRow As Integer
lastRow = ActiveSheet.UsedRange.Rows.Count
Dim c As Range
Dim d As Range
Application.ScreenUpdating = False
For Each c In Worksheets("Sheet11").Range("A2:A" & lastRow).Cells
found = False 'set flag here for cell
For Each d In Worksheets("Sheet12").Range("A2:A" & lastRow).Cells
If (InStr(1, d, c, 1) > 0) Then
c.Interior.Color = vbWhite
found = True
Exit For
End If
Next d
If Not found Then 'if cell do not match change the color
c.Interior.Color = vbRed
If Not flag Then flag = True 'change filter flag to true just once
End If
Next c
If flag Then 'check for filter flag
ActiveSheet.Range("A1:A" & lastRow).AutoFilter Field:=1, Criteria1:=RGB(255, 0 _
, 0), Operator:=xlFilterCellColor
End If
Application.ScreenUpdating = True
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.