如何使用vbscript在Excel中查找特定值的行号 [英] How to find the row number of a specific value in Excel using vbscript

查看:181
本文介绍了如何使用vbscript在Excel中查找特定值的行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

这是VBA在列中找到test2的第一个实例A的活动表。您可以根据需要调整字符串和工作表。如果整个单元格匹配,则仅计数为匹配项,例如test2222将不匹配。如果你想要,删除, lookat:= xlWhole 位:

  Sub FindFirstInstance()
Const WHAT_TO_FIND As String =test2
Dim ws As Excel.Worksheet
Dim FoundCell As Excel.Range

设置ws = ActiveSheet
Set FoundCell = ws.Range(A:A)。Find(what:= WHAT_TO_FIND,lookat:= xlWhole)
如果Not FoundCell不是,然后
MsgBox(WHAT_TO_FIND&
Else
MsgBox(WHAT_TO_FIND&not found)
End If
End Sub


I have an open Excel file and using VB Script, I need to search only column "A" in the Excel sheet until it matches a text string. When the script finds that match, I would like to see the row number of the cell where the match was found. Thanks for your helps in advance!

解决方案

This is VBA to find the first instance of "test2" in column A of the activesheet. You can adjust the string and worksheet accord to your needs. It only counts as a match if the whole cell matches, e.g., "test2222" won't match. If you want it to, remove the , lookat:=xlWhole bit:

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在Excel中查找特定值的行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆