查找 Excel 表中是否存在所给字符串宏
之前用到过的查找 Excel 表中是否存在所给字符串宏,给大家分享一下,保存为 bas 文件, 使用时把 excel 路径和字符串传入即可
Attribute VB_Name = “查数据”
Function FindStr(ByVal workbookPath,ByVal str)
Dim Sh As Worksheet
Dim wb As Workbook
Dim Loc As Range
Dim flag
flag = “no”
’ 初始化 (打开所需工作表)
Set wb = Workbooks.Open(workbookPath)
’ 遍历每个 sheet 页
For Each Sh In wb.Worksheets
’ 在当前 sheet 页有效使用区域内查找相应字符串
With Sh.UsedRange
Set Loc = .Cells.Find(What:=str)
If Not Loc Is Nothing Then
flag = “yes”
Exit For
End If
End With
Set Loc = Nothing
Next
FindStr = flag
’ 关闭工作表
wb.close
End Function
回帖内容已被屏蔽。
欢迎大佬
感谢分享