将两列的单元格以仅以特定字母开头的单独页面进行比较

我正在寻求创build一个macros函数,将比较两个不同的列在不同的工作表上,并显示find的匹配数(简单地写在Sheet2的单元格就足够了)。

但是,我不想在比较时在列中包含所有的单元格,我只想要考虑以字母O (大写)开头的单元格。

以下是我正在比较的数据的一个例子

工作表Sheet1:

 7 Work Request(s). OMEC/2014/253 OELC/2014/97 OELC/2014/98 OMEC/2014/318 OMEC/2014/398 OMEC/2014/468 OMEC/2014/439 10 Work Request(s). OELC/2013/904 OMEC/2013/3544 OMEC/2014/123 OMEC/2014/459 OMEC/2014/516 OMEC/2014/514 OELC/2014/160 OMEC/2014/542 OMEC/2014/543 OELC/2014/173 

Sheet2中:

 6 Work Request(s). OMEC/2014/253 OELC/2014/97 OELC/2014/98 OMEC/2014/398 OMEC/2014/468 OMEC/2014/440 7 Work Request(s). OELC/2013/904 OMEC/2013/3544 OMEC/2014/123 OMEC/2014/477 OMEC/2014/516 OMEC/2014/515 OELC/2014/160 

这个想法是将Sheet2Sheet1进行比较,并说明(在这种情况下)有10个相似之处

在表二的某处,我想要显示: Number of completed requests since last week: 10

7 Work Request(s). 或者类似的东西在比较时不得不被忽略,即使它出现在两列中,这就是为什么我build议只比较以O开始的数据,因为所有的相关数据都以O开头。

如果需要任何其他信息,请告诉我。

预先感谢您的任何帮助!

假设你的数据是非常简单的完成(没有装饰,明显复杂的单元格等),以下工作正常。

码:

 Sub CountSimilar() Dim WS0 As Worksheet, WS1 As Worksheet Dim R0 As Range, R1 As Range Dim C0 As Range, RFound As Range Dim NumFound As Long, StrFound As String With ThisWorkbook Set WS0 = .Sheets("Sheet1") 'Modify as necessary. Set WS1 = .Sheets("Sheet2") 'Modify as necessary. End With Set R0 = WS0.Range("A:A").SpecialCells(xlCellTypeConstants) 'Modify as necessary. Set R1 = WS1.Range("A:A").SpecialCells(xlCellTypeConstants) 'Modify as necessary. StrFound = "The following similarities were found:" & vbNewLine & vbNewLine NumFound = 0 For Each C0 In R0 If Left(C0.Value, 1) = "O" Then Set RFound = R1.Find(Trim(C0.Value)) If Not RFound Is Nothing Then StrFound = StrFound & C0.Value & vbNewLine NumFound = NumFound + 1 End If End If Next C0 StrFound = StrFound & vbNewLine StrFound = StrFound & "The total number of similarities found is " & NumFound & "." MsgBox StrFound End Sub 

截图:

在这里输入图像描述

让我们知道这是否有帮助。

进一步编辑:

码:

 Sub CountSimilarDeux() Dim WS0 As Worksheet, WS1 As Worksheet Dim R0 As Range, R1 As Range Dim C0 As Range, RFound As Range, C1 As Range Dim NumFound As Long, StrFound As String Dim NumSht2 As Long, NumDiff As Long With ThisWorkbook Set WS0 = .Sheets("Sheet1") 'Modify as necessary. Set WS1 = .Sheets("Sheet2") 'Modify as necessary. End With Set R0 = WS0.Range("A:A").SpecialCells(xlCellTypeConstants) 'Modify as necessary. Set R1 = WS1.Range("A:A").SpecialCells(xlCellTypeConstants) 'Modify as necessary. StrFound = "The following similarities were found:" & vbNewLine & vbNewLine NumFound = 0 For Each C0 In R0 If Left(C0.Value, 1) = "O" Then Set RFound = R1.Find(Trim(C0.Value)) If Not RFound Is Nothing Then StrFound = StrFound & C0.Value & vbNewLine NumFound = NumFound + 1 End If End If Next C0 NumSht2 = 0 For Each C1 In R1 If Left(C1.Value, 1) = "O" Then NumSht2 = NumSht2 + 1 End If Next C1 NumDiff = NumSht2 - NumFound StrFound = StrFound & vbNewLine StrFound = StrFound & "The total number of similarities found is " & NumFound & "." & vbNewLine StrFound = StrFound & "The total number of qualified strings in Sheet2 is " & NumSht2 & "." & vbNewLine StrFound = StrFound & "The difference is " & NumDiff & "." MsgBox StrFound End Sub 

截图:

在这里输入图像说明

让我们知道这是否有帮助。