就像我在评论中提到的那样,如果您知道所有分隔符是什么,那么它就是小菜一碟。
为了测试演示目的,我有这个简单的例子。
在此示例中,我将在 D 列中输出匹配项的单元格地址
由于这是一个示例/演示,因此我使用硬编码值来循环遍历列。要实际找到列中的最后一行,请参阅此链接
逻辑:
- 将分隔符
_
, .
,存储-
在字符串中
- 使用“,”作为分隔符将其拆分并将其存储在数组中
- 遍历列值时,只需遍历数组并将分隔符替换为“”
- 获得基本字符串后,只需使用
.Find
在 Col A 中搜索该字符串即可。
代码:
Option Explicit
'~~> This is the list for your separators separated by comman
Const sep As String = "_,.,-"
Sub Sample()
Dim ws As Worksheet
Dim MyAr
Dim SearchString As String
Dim aCell As Range
Dim i As Long, j As Long
'~~> Split the separator using "," and store it in an array
MyAr = Split(sep, ",")
'~~> Set this to the relevant workbook
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
'~~> Loop through Col C
For i = 1 To 3
If Len(Trim(.Range("C" & i).Value)) <> 0 Then
SearchString = .Range("C" & i).Value
'~~> Loop through the array and replace all separator by ""
For j = LBound(MyAr) To UBound(MyAr)
SearchString = Replace(SearchString, MyAr(j), "")
Next
'~~> Find the result txt in Col B
Set aCell = .Columns(2).Find(What:=Trim(SearchString), LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
'~~> If found then output to cell D
If Not aCell Is Nothing Then
.Range("D" & i).Value = "Found in Cell " & aCell.Address
End If
End If
Next i
End With
End Sub
截屏: