我有一个打开的 Excel 文件并使用 VB 脚本,我只需要在 Excel 工作表中搜索列“A”,直到它与文本字符串匹配。当脚本找到匹配项时,我想查看找到匹配项的单元格的行号。提前感谢您的帮助!
问问题
97166 次
2 回答
20
这是在活动表的 A 列中查找“test2”的第一个实例的 VBA。您可以根据需要调整字符串和工作表。仅当整个单元格匹配时才算作匹配,例如“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
于 2012-04-30T20:19:24.083 回答
-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
于 2013-01-31T19:31:30.153 回答