1

让 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。而且我还需要每天更改日期来执行此操作。

4

1 回答 1

0

假设您的数据放在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

假设和细节:

  1. 数据范围实际上是无限的。
  2. 两个文件都放在同一个文件夹中。名称和路径应在代码中定义。
  3. “突出显示”是使用单元格填充完成的,.Interior.Color = RGB(0, 255, 0)根据需要进行更改。
  4. 仅检查第一次也是唯一一次出现的键字符串是否更改(基于输入)。

示例文件也被共享:https ://www.dropbox.com/s/vqkulovmg98yaih/DailyDataHighlight.xlsm

希望这对学习VBA也有帮助)

于 2013-01-29T10:14:46.427 回答