我正在尝试设计一个宏以在Excel中搜索多个字符串。
我有以下代码,可在excel中搜索单词“ techno”,但我需要在代码中包含一个变量,以便我可以搜索多个单词,例如“ Techno”,“ electromagnetic”,“ waves”,等等。我无法为此情况创建循环。
有人可以提出解决此问题的方法吗?下面的代码可以正常工作,但是只需要进行调整即可在搜索中包含多个字符串。
子SearchFolders()
Dim xFso作为对象
将xFld作为对象昏暗
Dim xStrSearch作为字符串
将xStrPath设置为字符串
Dim xStrFile作为字符串
将xOut视作工作表
将xWb用作工作簿
将xWk用作工作表
Dim xRow as Long
Dim xFound作为范围
Dim xStrAddress作为字符串
将xFileDialog复制为FileDialog
将xUpdate更新为布尔值
Dim xCount只要长
将myArray当作变体'shan
将myCounter设为Long'shan
myArray = Array(“ techno”,“ magnetic”,“ laser”,“ trent”)'shan
发生错误时转到TorrHandler
设置xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)
xFileDialog.AllowMultiSelect = False
xFileDialog.Title =“选择炮台”
如果xFileDialog.Show = -1然后
xStrPath = xFileDialog.SelectedItems(1)
如果结束
如果xStrPath =“”然后退出Sub
'xStrSearch =“我们的]
xUpdate = Application.ScreenUpdating
Application.ScreenUpdating = False
设置xOut = Worksheets.Add
对于myCounter = 0到UBound(myArray)'shan
MsgBox myCounter&“是计数编号。” '山
xStrSearch = myArray(myCounter)'shan
[MsgBox xStrSearch&“是值fr字符串搜索”'shan
xRow = 1
使用xOut
。Cells(xRow,1)=“工作簿”
。Cells(xRow,2)=“工作表”
。Cells(xRow,3)=“细胞”
。Cells(xRow,4)=“单元格中的文本”
设置xFso = CreateObject(“ Scripting.FileSystemObject”)
设置xFld = xFso.GetFolder(xStrPath)
xStrFile = Dir(xStrPath&“ * .xls *”)
Do While xStrFile <>“”
设置xWb = Workbooks.Open(文件名:= xStrPath和“ \”&xStrFile,UpdateLinks:= 0,ReadOnly:= True,AddToMRU:= False)
对于xWb.Worksheets中的每个xWk
设置xFound = xWk.UsedRange.Find(xStrSearch)
[MsgBox xFound&“是找到的字符串”'shan
如果不是xFound则什么也没有
xStrAddress = xFound.Address
如果结束
执行
如果xFound什么都没有,则>]
退出
其他
xCount = xCount + 1
[MsgBox xCount&“是字符串的计数”'shan
xRow = xRow + 1
。Cells(xRow,1)= xWb.Name
。Cells(xRow,2)= xWk.Name
。Cells(xRow,3)= xFound.Address
。Cells(xRow,4)= xFound.Value
如果结束
设置xFound = xWk.Cells.FindNext(After:= xFound)
MsgBox xFound&“下一个字符串”'shan
MsgBox xStrAddress&“是地址”'shan
MsgBox xFound.Address&“是找到的地址”'shan
循环xStrAddress <> xFound.Address'要检查xStrAddress的填充方式,还是需要将其声明为来自excel的帮助,请指出
myCounter = myCounter + 1
下一个
xWb.Close(False)
xStrFile =目录
循环
.. Columns(“ A:D”)。EntireColumn.AutoFit
结尾为
下一个myCounter'shan
MsgBox xCount和“已找到单元格”,“ Kutools for Excel”
ExitHandler:
Set xOut = Nothing
Set xWk = Nothing
Set xWb = Nothing
Set xFld = Nothing
Set xFso = Nothing
Application.ScreenUpdating = xUpdate
退出子
ErrHandler:
MsgBox错误说明,vbExclamation
恢复ExitHandler
结束子
我正在尝试设计一个宏以在Excel中搜索多个字符串。我有以下代码在excel中搜索“ techno”一词,但我需要在代码中包含一个变量,因此...
如果要搜索的字符串始终相同,则将它们硬编码为数组,然后遍历数组元素以搜索每个字符串,如下所示: