计算行数,直到找到特定的字符串 [英] Count Rows Until Finding Specific String
问题描述
我试图使用Excel VBA对行数进行计数,直到命中特定的字符串并设置与之相等的变量为止.
I am trying to use Excel VBA to count the number of rows until I hit a specific string and set a variable equal to it.
类似的东西:
Dim i as Integer
i = Worksheets("Scope Phase Document").Range("A1", Range("A1").End(xlDown)).Find("FACTS - What are we measuring?").Count
我知道这不是正确的语法,并且我可能会缺少其他内容,但是仅使用我目前知道的不同功能,这就是我希望能够解决的问题.我得到
I know this isn't the correct syntax and I'm probably missing other stuff, but just using the different functions I currently know, this is what I would hope would do the trick. I get
运行时错误"91"显示对象变量或不带块变量设置
Run-time error '91' saying "Object variable or With block variable not set
我尝试了几种不同的方法,但是无法找出不会导致错误的方法.
I have tried a few different ways of doing it, but can't figure out a way that doesn't result in an error.
所以我想从A1开始,对所有行进行递减计数,直到到达特定的字符串事实-我们要测量的是什么?".
So I want to start at A1 and count all the rows down until I reach the specific string "FACTS - What are we measuring?".
任何帮助将不胜感激!
推荐答案
我更喜欢MATCH,但是如果找不到匹配项,则会引发错误.因此,我们需要对此进行测试:
I prefer MATCH, but if the match is not found it throws an error. So we need to test for that:
Dim i As Long
i = 0
On Error Resume Next
i = Application.WorksheetFunction.Match("FACTS - What are we measuring?", ActiveSheet.Range("A:A"), 0)
On Error GoTo 0
If i > 0 Then
' do you stuff with i
End If
这篇关于计算行数,直到找到特定的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!