VBA - 查找包含特定文本的第一行和最后一行 [英] VBA - Find first and last row containing specific text

查看:2193
本文介绍了VBA - 查找包含特定文本的第一行和最后一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个长(2,000 +行)不同值的列表。我试图找到每个值的第一行和最后一行。作为示例(注意:所有数据都在一列中):

I have a long (2,000 + rows) list of different values. I am trying to find the first and last row for each value. As an example (note: all data is in one column):

Bats
Bats
Bats
Bats
Bats
Bats
Fun
Fun
Fun
Fun
Bases
Bases
Bases
Bases
Bases
Bases
Bases
Bases
Bases
Bases
Bases
Balls
Balls
Balls
Balls
Balls
Balls
Balls
Balls

所以,我想知道最快的方法是找到第一行(从顶部开始)和包含每个单词的最后一行。所以,蝙蝠开始第1行,结束于第6行。乐趣开始第7行,结束于第10行。

So, I was wondering what the quickest way to find the FIRST row (starting at the top) and LAST row containing each word is. So, bats starts row 1, ends at row 6. Fun starts row 7, ends at row 10.

任何想法,以更快的方式来做到这一点,除了循环,我将每个单元格与上一个单元格进行比较,如果它们不同,则添加一个行变量?

Any ideas for a quicker way to do this, other than a loop where I compare each cell to the previous, and add a row variable if they're different?

非常感谢任何建议/帮助!

Thanks so much for any advice/help!

编辑:在VBA中寻找如何做到这一点。

edit: looking for how to do so in VBA.

推荐答案

- VBA风格我可以使用find()来帮助:

I figured it out - VBA style. I can just use find() to help out:

Dim batStartRow as Integer, batEndRow as Integer
With Sheets("Sheet1")
    batStartRow = .Range("A:A").Find(what:="bats", after:=.Range("A1")).Row
    batEndRow = .Range("A:A").Find(what:="bats",after:=.Range("A1"), searchdirection:=xlPrevious).Row
End With

然后用其他单词替换蝙蝠,它会奏效。

Then replace "bats" with the other words, and it'll work.

编辑:您可能需要添加 LookIn:= xlValues 限定符,具体取决于正在搜索的信息/数据。

You may need to add the LookIn:=xlValues qualifier too, depending on the info/data being searched.

这篇关于VBA - 查找包含特定文本的第一行和最后一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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