Excel根据连续匹配字符判断两个单元格是否匹配
Excel determine if two cells match based on consecutive matching characters
如何在两个单元格之间进行部分字符串匹配,其中部分字符串可以位于单元格中的任意位置。
示例:电池 1 可能有 AXG58934x0,电池 2 可能有 05893400
或者 Cell 1 可能有 5893400A,Cell 2 可能有 X1000000589340000y
单元格应匹配“58934”部分。
希望 Excel 查看单元格 1 和单元格 2,如果找到一个字符匹配,则查看下一个字符,如果匹配,则查看下一个字符,如果有 5 个连续字符匹配然后 return 这个词 "match"。
[VBA
解决方案]实施:
- 按 Alt + F11 打开 Visual Basic
- 在
VBAProject
查看器中找到您正在处理的图书
- 右击 & Select
Insert Module
- 将下面的代码粘贴到代码中space
- 返回 excel,您可以像调用任何函数一样调用该函数
=CSTMATCH()
该函数接受 2 个输入(第一个字符串和第二个字符串),如下图所示
Option Explicit
Public Function CSTMatch(Target1 As Range, Target2 As Range) As Boolean
CSTMatch = False
Dim String1 As String, String2 As String, i As Long
'The goal here is to assign the larger String to the variable String1
If Len(Target1) >= Len(Target2) Then
String1 = Target1
String2 = Target2
Else
String1 = Target2
String2 = Target1
End If
For i = 1 To Len(String1) - 4
If Mid(String1, i, 5) <> "00000" Then
If InStr(String2, Mid(String1, i, 5)) Then
CSTMatch = True
Exit Function
End If
End If
Next i
End Function
下面是 UDF
的 inputs/outputs 的例子
灵感来自@urdearboy
这将为您提供最短字符串中连续字符与最长字符串中连续字符的比率
Option Explicit
Public Function CSTMatch2(Target1 As Range, Target2 As Range) As Double
CSTMatch2 = 0
Dim String1 As String, String2 As String, i As Long, j As Long, noChar As Long
noChar = 0
'The goal here is to assign the larger String to the variable String1
If Target1 = Target2 Then
CSTMatch2 = 1
Exit Function
End If
If Len(Target1) >= Len(Target2) Then
String1 = Target1
String2 = Target2
Else
String1 = Target2
String2 = Target1
End If
For j = 1 To Len(String2)
For i = 1 To Len(String1) - j
If InStr(String2, Mid(String1, i, j)) Then
noChar = noChar + 1
Exit For
End If
Next i
Next j
Debug.Print noChar, Len(String1), Len(String2)
CSTMatch2 = (noChar) / (Len(String1))
End Function
样本:
如何在两个单元格之间进行部分字符串匹配,其中部分字符串可以位于单元格中的任意位置。
示例:电池 1 可能有 AXG58934x0,电池 2 可能有 05893400 或者 Cell 1 可能有 5893400A,Cell 2 可能有 X1000000589340000y 单元格应匹配“58934”部分。
希望 Excel 查看单元格 1 和单元格 2,如果找到一个字符匹配,则查看下一个字符,如果匹配,则查看下一个字符,如果有 5 个连续字符匹配然后 return 这个词 "match"。
[VBA
解决方案]实施:
- 按 Alt + F11 打开 Visual Basic
- 在
VBAProject
查看器中找到您正在处理的图书 - 右击 & Select
Insert Module
- 将下面的代码粘贴到代码中space
- 返回 excel,您可以像调用任何函数一样调用该函数
=CSTMATCH()
该函数接受 2 个输入(第一个字符串和第二个字符串),如下图所示
Option Explicit
Public Function CSTMatch(Target1 As Range, Target2 As Range) As Boolean
CSTMatch = False
Dim String1 As String, String2 As String, i As Long
'The goal here is to assign the larger String to the variable String1
If Len(Target1) >= Len(Target2) Then
String1 = Target1
String2 = Target2
Else
String1 = Target2
String2 = Target1
End If
For i = 1 To Len(String1) - 4
If Mid(String1, i, 5) <> "00000" Then
If InStr(String2, Mid(String1, i, 5)) Then
CSTMatch = True
Exit Function
End If
End If
Next i
End Function
下面是 UDF
的 inputs/outputs 的例子
灵感来自@urdearboy
这将为您提供最短字符串中连续字符与最长字符串中连续字符的比率
Option Explicit
Public Function CSTMatch2(Target1 As Range, Target2 As Range) As Double
CSTMatch2 = 0
Dim String1 As String, String2 As String, i As Long, j As Long, noChar As Long
noChar = 0
'The goal here is to assign the larger String to the variable String1
If Target1 = Target2 Then
CSTMatch2 = 1
Exit Function
End If
If Len(Target1) >= Len(Target2) Then
String1 = Target1
String2 = Target2
Else
String1 = Target2
String2 = Target1
End If
For j = 1 To Len(String2)
For i = 1 To Len(String1) - j
If InStr(String2, Mid(String1, i, j)) Then
noChar = noChar + 1
Exit For
End If
Next i
Next j
Debug.Print noChar, Len(String1), Len(String2)
CSTMatch2 = (noChar) / (Len(String1))
End Function
样本: