Excel – 查找“看起来像”的值

我有一个Excel表格的Excel工作簿。 在第一个工作表“用户”我有用户数据,名字,姓氏,电子邮件等所有整齐地从一个CSV文件分裂。 在其他工作表中,我有一些名字,需要从“用户”工作表的电子邮件。

问题是,所有其他工作表上的名称都在一个单元格中,名字和姓氏都是一样的,而在用户表单中它们是分开的。 此外,在其他表中可能会写成“迈克安德森”,“迈克,安德森”,甚至“安德森,迈克”。

有没有人有一个macros/ VBA脚本/公式的想法,这将帮助我find并复制相应的电子邮件?

为了检查Mike AndersonMike, Anderson甚至Anderson, Mike ,你可以使用.Find.FindNext

看到这个例子

逻辑 :使用Excel的内置.Find方法findMike ,一旦发现,只要检查该单元格是否也有Anderson

 Sub Sample() Dim oRange As Range, aCell As Range, bCell As Range Dim ws As Worksheet Dim SearchString As String, FoundAt As String On Error GoTo Err Set ws = Worksheets("Sheet1") Set oRange = ws.Columns(1) SearchString = "Mike" Set aCell = oRange.Find(What:=SearchString, LookIn:=xlValues, _ LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False) If Not aCell Is Nothing Then Set bCell = aCell If InStr(1, aCell.Value, "Anderson", vbTextCompare) Then _ FoundAt = aCell.Address Do Set aCell = oRange.FindNext(After:=aCell) If Not aCell Is Nothing Then If aCell.Address = bCell.Address Then Exit Do If InStr(1, aCell.Value, "Anderson", vbTextCompare) Then _ FoundAt = FoundAt & ", " & aCell.Address Else Exit Do End If Loop Else MsgBox SearchString & " not Found" Exit Sub End If MsgBox "The Search String has been found these locations: " & FoundAt Exit Sub Err: MsgBox Err.Description End Sub 

截图

在这里输入图像说明

更多关于.Find.Findnext 在这里 。

你可以使用带有通配符的VBA LIKE运算符?

 If activecell.text LIKE "*Paul*" then ... 

另外,正如Floris指出的那样,您需要在模块顶部设置Option Compare Text ,以确保您的testing不区分大小写

通过将文本框和选项button添加到工作簿的第一张工作表中,可以在所有工作簿中轻松findsearch值。

在这里input图像说明

通过选项button,文本框中的值可以被search为全部或部分两种types:

 If Sheets(1).OptionButton1 = True Then Set Firstcell = Cells.Find(What:=Sheets(1).TxtSearch, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False) Else Set Firstcell = Cells.Find(What:=Sheets(1).TxtSearch, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False) End If 

我也在模板编码中使用了Find&FindNext方法

 If Not Firstcell Is Nothing Then Firstcell.Activate Firstcell.Interior.ColorIndex = 19 With Sheets("New_Report").Range("A1") .Value = "Addresses Of The Found Results" .Interior.ColorIndex = 19 End With Sheets("New_Report").Range("A:A").EntireColumn.AutoFit Sheets("New_Report").Range("A" & Rows.Count).End(xlUp).Offset(1, 0) = oSheet.Name & "!" & Firstcell.Address(False, False) Call Create_Hyperlinks 'Hyperlinks are generated in New Report Sheet If MsgBox("Found " & Chr(34) & Sheets(1).TxtSearch & Chr(34) & " in " & oSheet.Name & "!" & Firstcell.Address & vbLf & "Do You Want To Continue?", vbExclamation + vbYesNo) = vbNo Then Exit Sub: End If While (Not NextCell Is Nothing) And (Not NextCell.Address = Firstcell.Address) counter = counter + 1 Firstcell.Interior.ColorIndex = xlNone Set NextCell = Cells.FindNext(After:=ActiveCell) If NextCell.Row = 2 Then Set NextCell = Range(Cells(3, 1), Cells(Cells(Rows.Count, 1).End(xlUp).Row, LastColumn)).FindNext(After:=ActiveCell) End If If Not NextCell.Address = Firstcell.Address Then NextCell.Activate NextCell.Interior.ColorIndex = 19 Sheets("New_Report").Range("A" & Rows.Count).End(xlUp).Offset(1, 0) = oSheet.Name & "!" & NextCell.Address(False, False) Call Create_Hyperlinks If MsgBox("Found " & Chr(34) & Sheets(1).TxtSearch & Chr(34) & " in " & oSheet.Name & "!" & NextCell.Address & vbLf & "Do You Want To Continue?", vbExclamation + vbYesNo) = vbNo Then Exit Sub: End If End If 'If Not NextCell.Address = Firstcell.Address Then NextCell.Interior.ColorIndex = xlNone Wend End If Next oSheet End If 

所有结果在生成的报告表中以不同的函数列出为超链接。