我有一个打开的Excel文件并使用VB脚本,我只需要在Excel工作表中搜索列“A”,直到它与文本字符串匹配。当脚本找到匹配项时,我希望看到找到匹配项的单元格的行号。 感谢您的帮助!
答案 0 :(得分:20)
这是VBA在活动表的A列中找到“test2”的第一个实例。您可以根据需要调整字符串和工作表。如果整个单元格匹配,则仅计为匹配,例如,“test2222”将不匹配。如果需要,请删除lookat:=xlWhole
位:
Sub FindFirstInstance()
Const WHAT_TO_FIND As String = "test2"
Dim ws As Excel.Worksheet
Dim FoundCell As Excel.Range
Set ws = ActiveSheet
Set FoundCell = ws.Range("A:A").Find(what:=WHAT_TO_FIND, lookat:=xlWhole)
If Not FoundCell Is Nothing Then
MsgBox (WHAT_TO_FIND & " found in row: " & FoundCell.Row)
Else
MsgBox (WHAT_TO_FIND & " not found")
End If
End Sub
答案 1 :(得分:-2)
感谢您的样品。下面是VBScript
Dim FSO, oExcel, oData, FoundCell, WHAT_TO_FIND, File_Path
WHAT_TO_FIND = "Report Summary"
File_Path = "\\[Server]\[Drive$]\[Folder]\Data.xls"
Set FSO = CreateObject("Scripting.FileSystemObject")
Set oExcel = CreateObject("Excel.Application")
Set oData = oExcel.Workbooks.Open(File_Path)
Set FoundCell = oData.Worksheets("Sheet1").Range("A4:A20000").Find(WHAT_TO_FIND)
If Not FoundCell Is Nothing Then
MsgBox (WHAT_TO_FIND & " found in row: " & FoundCell.Row)
Else
MsgBox (WHAT_TO_FIND & " not found")
End If
Set File_Path = nothing
Set WHAT_TO_FIND = nothing
Set FoundCell = nothing
Set oData = Nothing
Set oExcel = Nothing
Set FSO = Nothing