如何使用vbscript在Excel中查找特定值的行号

我有一个打开的Excel文件,并使用VB脚本,我需要在Excel工作表中只search列“A”,直到它匹配一个文本string。 当脚本find匹配的时候,我想查看find匹配的单元格的行号。 感谢您的帮助提前!

这是VBA在activesheet的A列中find“test2”的第一个实例。 您可以根据需要调整string和工作表。 如果整个单元格匹配,它只能算作匹配,例如“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 

谢谢你的样品。 下面是在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