Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用vbscript在Excel中查找特定值的行号_Excel_Vbscript - Fatal编程技术网

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

如何使用vbscript在Excel中查找特定值的行号,excel,vbscript,Excel,Vbscript,我有一个打开的Excel文件,使用VB脚本,我只需要搜索Excel工作表中的“A”列,直到它匹配文本字符串。当脚本找到匹配项时,我希望看到找到匹配项的单元格的行号。 提前感谢您的帮助 这是VBA在activesheet的A列中查找“test2”的第一个实例。您可以根据需要调整字符串和工作表。仅当整个单元格匹配时才算作匹配,例如,“test2222”不匹配。如果需要,请删除,lookat:=xlWhole位: Sub FindFirstInstance() Const WHAT_TO_FIND A

我有一个打开的Excel文件,使用VB脚本,我只需要搜索Excel工作表中的“A”列,直到它匹配文本字符串。当脚本找到匹配项时,我希望看到找到匹配项的单元格的行号。
提前感谢您的帮助

这是VBA在activesheet的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

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

我不知道。由于您的标记包含
excelvba
我给了您该代码。有时人们会交替使用“vbscript”和“vba”。@buri kuri该代码需要对
vbscript
进行重大调整,即自动化Excel,删除变量的显式标注,为
xlother
添加常量等。你需要更具体地了解你想要什么朋友,我用Doug的解解决了find问题,这是另一个问题。当我尝试从A列搜索“1/1/2011”时,它会带来“11/1/2011”行编号,我不想这样匹配。如何保持字符的精确匹配?顺便说一句,我使用了vbscript,下面是一行-->Set FoundCell=objSheet.Range(“A:A”)。Find(“1/1/2011”)您需要在其中获取
LookAt:=xlWhole
参数。这就是@brettdj所说的“常量”。这是一个猜测,但请尝试
objSheet.Range(“a:a”)。查找(“1/1/2011”、、、、、、、)
。我认为这是可行的,因为
LookAt
是要查找的第四个参数,1是与xlWhole等效的数字(常量)。这是我问题的答案-->objSheet.Range(“A:A”)。Find(“1/1/2011”、,,1)显示您迄今为止拥有的代码-添加到其中比猜测您真正指的是VBA还是vbscript更容易。正如Doug提到的,它们有时被用来表示相同的意思。-1对于:未使用的FSO,()当将MsgBox作为子对象调用时,使用Set作为字符串File_Path和WHAT_to_FIND。