以下是使用VBA在Excel工作表中查找字符串的示例代码:

Sub findString()

Dim searchString As String Dim searchRange As Range Dim foundCell As Range

searchString = InputBox("Please enter the string you want to find:", "Find String")

If searchString = "" Then Exit Sub

Set searchRange = ActiveSheet.Cells

Set foundCell = searchRange.Find(what:=searchString, LookIn:=xlValues, LookAt:=xlPart, MatchCase:=False)

If Not foundCell Is Nothing Then foundCell.Activate MsgBox "The string '" & searchString & "' was found in cell " & foundCell.Address Else MsgBox "The string '" & searchString & "' could not be found in the worksheet." End If

End Sub

这个代码将提示用户输入要查找的字符串,然后在当前活动工作表的所有单元格中查找该字符串。如果找到了该字符串,它将激活包含该字符串的单元格,并显示一个消息框,指示该字符串在哪个单元格中被找到。如果在整个工作表中找不到该字符串,则显示一个消息框,指示该字符串无法找到

使用VBA在EXCEL工作表内查找字符串

原文地址: https://www.cveoy.top/t/topic/hvBL 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录