参考文档:
https://blog.csdn.net/Hotty_Lee/article/details/78602389
相似举例:
在VBA中实现Excel模糊查找功能可以通过以下代码实现:
Sub FuzzySearch()
Dim searchValue As String
Dim cell As Range
searchValue = InputBox("请输入要查找的值:")
For Each cell In ActiveSheet.UsedRange
If InStr(1, cell.Value, searchValue, vbTextCompare) > 0 Then
cell.Select
Exit Sub
End If
Next cell
MsgBox "未找到匹配项。"
End Sub
该代码会弹出一个输入框,让用户输入要查找的值。然后,它会遍历活动工作表中的所有单元格,如果找到包含该值的单元格,则选择该单元格并退出子程序。如果遍历完所有单元格后仍未找到匹配项,则弹出一个消息框提示用户未找到匹配项。
在这个代码中,InStr函数用于查找字符串中是否包含指定的子字符串。vbTextCompare参数表示比较时忽略大小写。如果找到匹配项,就选择该单元格并退出子程序。如果遍历完所有单元格后仍未找到匹配项,则弹出一个消息框提示用户未找到匹配项。
=============================
VBA宏如何返回某个匹配到的单元格所在工作簿的名称?
参考答案1:
你可以使用以下的 VBA 宏来返回某个匹配到的单元格所在工作簿的名称:
Function GetWorkbookName(cell As Range) As String
' This function returns the name of the workbook that contains the specified cell
' Check if the cell is not empty
If cell.Value <>"" Then
' Get the name of the workbook that contains the cell
GetWorkbookName = cell.Workbook.Name
End If
End Function
你可以在你的 VBA 代码中调用这个函数,例如:
Dim wbName As String
wbName = GetWorkbookName(Range("A1"))
MsgBox "The workbook name is: "& wbName
这段代码将返回单元格 A1所在工作簿的名称,并在消息框中显示。
参考答案2:
可以使用以下VBA代码来返回某个匹配到的单元格所在工作簿的名称:
Function GetWorkbookName(cellValue As String) As String
Dim wb As Workbook
For Each wb In Application.Workbooks
If Not wb Is ThisWorkbook Then
If Not wb.Worksheets(1).Range("A1").Value = "" Then
If wb.Worksheets(1).Range("A1").Value = cellValue Then
GetWorkbookName = wb.Name
Exit Function
End If
End If
End If
Next wb
End Function
在上面的代码中,我们定义了一个名为 GetWorkbookName
的函数,该函数接受一个字符串参数 cellValue
,表示要匹配的单元格的值。然后,我们循环遍历所有打开的工作簿,检查它们的第一个工作表的A1单元格是否与 cellValue
匹配。如果找到匹配项,则返回该工作簿的名称。如果没有找到匹配项,则返回一个空字符串。