我准备了一个代码来比较不同工作簿的两个列值,如果Result列值大于Reference列值,则结果列值应该着色为黄色。代码无法正常使用所有值。即使在与参考值比较时结果值更大,某些值也不会变为彩色。请在下面找到我的代码 - 代码将检查天气A列数据(参考)出现在结果簿A列中 - 如果现在将结果的Referance的相应B列数据复制到F列 - 比较E(结果值)和F列,如果E列值大于F,则将其标记为黄色(E列)
Sub ResultComparison()
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim y As Integer
Dim sht As Worksheet
Set wb2 = Workbooks.Open("D:\Comparison\ReferanceResult.xlsx")
Set wb1 = Workbooks.Open("D:\Comparison\Results.xlsx")
Set wb1sht = wb1.Worksheets("Measurements")
Set wb2sht = wb2.Worksheets("ReferanceMeasurement")
LastRowWb1 = wb1sht.Cells(wb1sht.Rows.Count, "A").End(xlUp).Row
LastRowWb2 = wb2sht.Cells(wb2sht.Rows.Count, "A").End(xlUp).Row
For y = 2 To LastRowWb2
'Fetching the first value from Referance book
findMe = wb2sht.Range("A" & y).Value
With wb1sht.Range("A2:A" & LastRowWb1)
Set oFound = .Find(findMe)
If Not oFound Is Nothing Then
'If found the Referance book value in Result book.Copying the B column value to adajuscent column F of Result.xlsx
wb1sht.Range("F" & oFound.Row).Value = wb2sht.Range("B" & y).Value
' Comparing the values(E column value to F column value)
If wb1sht.Range("E" & oFound.Row).Value > wb1sht.Range("F" & oFound.Row).Value Then
wb1sht.Range("E" & oFound.Row).Interior.Color = vbYellow
End If
End If
End With
Next
wb1.Close SaveChanges:=True
wb2.Close SaveChanges:=False
End Sub
给定代码的输出:
复制到Result.xlsx之前的参考值: