让 18jan.xlsx 包含以下数据:
ABC 10
XYZ 20
PQR 30
STU 40
DEF 50
HIJ 60
KLM 70
19jan.xlsx 包含:
XYZ 20
ABC 15
STU 40
DEF 50
HIJ 65
PQR 30
KLM 70
我需要比较它们并突出显示更改,例如此处应突出显示 ABC 前面的 15。而且我还需要每天更改日期来执行此操作。
假设您的数据放在A:B
从第 1 行开始的列中,以下代码应该可以完成这项工作:
Sub DailyDataHighlight()
Dim InputFolder As String
Dim OldExcel As String
Dim NewExcel As String
Dim i As Long
Dim OldString As String
Dim OldValue As String
Dim NewValue As String
Dim SearchResult As Range
InputFolder = "D:\DOCUMENTS\"
OldExcel = "18jan.xlsx"
NewExcel = "19jan.xlsx"
Application.ScreenUpdating = False
Application.Workbooks.Open (InputFolder & OldExcel)
Application.Workbooks.Open (InputFolder & NewExcel)
For i = 1 To Workbooks(OldExcel).ActiveSheet.Range("A1").CurrentRegion.Rows.Count
OldString = Workbooks(OldExcel).ActiveSheet.Cells(i, 1).Text
OldValue = Workbooks(OldExcel).ActiveSheet.Cells(i, 2).Text
Set SearchResult = Workbooks(NewExcel).ActiveSheet.Range("A1").CurrentRegion
Set SearchResult = SearchResult.Find(OldString, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=True)
If Not SearchResult Is Nothing Then
NewValue = SearchResult.Offset(0, 1).Text
If NewValue <> OldValue Then SearchResult.Offset(0, 1).Interior.Color = RGB(0, 255, 0)
End If
Next i
Workbooks(NewExcel).Save
Application.ScreenUpdating = True
End Sub
假设和细节:
.Interior.Color = RGB(0, 255, 0)
根据需要进行更改。示例文件也被共享:https ://www.dropbox.com/s/vqkulovmg98yaih/DailyDataHighlight.xlsm
希望这对学习VBA也有帮助)