【问题标题】:Comparing text within 2 columns in different workbooks比较不同工作簿中 2 ​​列中的文本
【发布时间】: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
      

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    您正在混合一些东西。即范围对象(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
    

    【讨论】:

    • 嗯,它给了我一个错误: If StrComp(Source_Data.Cells(j, 2).Value, Target_Data.Cells(i, 6).Value, CompareMethod.Text) = 0 Then ,它给了我一个对象所需的错误
    • 我使用以下方法修复了它: If StrComp(Source_Data.Cells(j, 2).Value, Target_Data.Cells(i, 6).Value, vbTextCompare) = 0 然后而不是与 CompareMethod 一起使用.Text
    • 哦,是的。我想知道 Comparemethod.Text 的用途是什么,但我猜你在 Class 模块或其他东西中有一些东西......我会更新我的答案。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多