【发布时间】:2016-05-11 20:39:39
【问题描述】:
我正在尝试比较两列中的所有单元格,每一列在不同的工作簿中。 单元格包含文本和数字,如果两个单元格(每个单元格位于不同的工作簿中)不同,我希望其中一个单元格突出显示/着色/填充。
任务:
1.1 - Cell1 = 嗨
1.1 - Cell2 = 嗨
所以这里不需要高亮,两个值是相等的
1.2 - Cell1 = 你好
1.2 - Cell2 = 你好
这里需要高亮,两个值不相等
注意:Cell1 和 Cell2 位于不同的工作簿中
这是我目前的代码:
Sub DescriptionDiscrepency()
- 将文件的位置设置为对象
-
目标路径在我的代码中是多余的,但可能对你们有用
Target_Path = "C:\Users\Example.xlsm" Set Target_Workbook = Workbooks("Example.xlsm") Target_Workbook.Sheets("Sheet1").Unprotect Password:="****" Set Source_Workbook = Workbooks("Example2.xlsm") Source_Workbook.Sheets("Sheet1").Unprotect Password:="*****"-
从目标文件中读取数据以查看源文件是否匹配
Target_Data = Target_Workbook.Sheets("Sheet1").Cells(2, 6).CurrentRegion.Rows.Count Source_Data = Source_Workbook.Sheets("Sheet1`").Cells(5, 2).CurrentRegion.Rows.Count 如果我们的状态跟踪器中的 CAT 描述不同,则突出显示
-
这部分不起作用
For i = 1 To lastRow For j = 1 To lastRow If Source_Data.Cells(j, 1).Value <> "" Then If StrComp(Source_Data.Cells(j, 2).Value, Target_Data.Cells(i, 6).Value, CompareMethod.Text) = 0 Then Source_Data.Cells(j, 2).Interior.ColorIndex = RGB(255, 255, 255) Source_Data.Cells(j, 2).Font.Color = RGB(0, 0, 0) Else Source_Data.Cells(j, 2).Interior.ColorIndex = RGB(0, 0, 0) Source_Data.Cells(j, 2).Font.Color = RGB(255, 199, 206) End If End If Next j Next I End Sub
-
【问题讨论】: