比较不同工作簿中的2列内的文本

我试图比较两列中的所有单元格,不同工作簿中的每列。 单元格包含文本和数字,如果两个单元格(每个在不同的工作簿中)是不同的,我希望其中一个单元格被突出显示/着色/填充。

任务:

1.1 – Cell1 =嗨

1.1 – Cell2 =嗨

所以这里不需要强调,两个值都是相等的

1.2 – Cell1 =你好

1.2 – Cell2 = Hellod

在这里需要强调,两个值是不相等的

注意:Cell1和Cell2都在不同的工作簿中

这是我到目前为止的代码:

Sub DescriptionDiscrepency() 
  • 将文件的位置设置为对象
  • 目标path在我的代码中是多余的,但可能对你们有用

     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 

你正在混合一些东西。 即范围对象(在Excel中的实际单元格)和行计数(简单的数字)。

试试像这样:

 Set Target_Data = Target_Workbook.Sheets("Sheet1").Cells(2, 6).CurrentRegion Set Source_Data = Source_Workbook.Sheets("Sheet1`").Cells(5, 2).CurrentRegion For i = 1 To Target_Data.Rows.Count For j = 1 To Source_Data.Rows.Count If Source_Data.Cells(j, 1).Value <> "" Then If StrComp(Source_Data.Cells(j, 2).Value, Target_Data.Cells(i, 6).Value, vbTextCompare) = 0 Then Source_Data.Cells(j, 2).Interior.Color = RGB(255, 255, 255) Source_Data.Cells(j, 2).Font.Color = RGB(0, 0, 0) Else Source_Data.Cells(j, 2).Interior.Color = 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