vba - 为什么这段代码不起作用?

时间:2017-06-15 04:02:16

标签: excel-vba vba excel

我有这个功能:它应该找到列中的所有单元格,其中包含单词Day,后跟连续的整数,即“第1天”,然后是“第2天”等,然后将值和地址存储在数组中,以便我可以稍后访问。我无法让它工作,我似乎不断收到错误:“第一次查找后代码执行被中断了。”请帮忙!

dim i, lastrow as long
dim dayaddresses() as string
dim findafter as Range
Dim vals() as string
Dim firstfound, foundrow as variant

Sub FindDays()

i=0  
b="0"
lastrow = worksheets("Data").usedrange.rows.count

With worksheets("Data").Range("O4:O" & lastrow)
  Set findafter = Range("A1")
  Do
  b=b+1
  Set foundrow = .Find("Day " & b, lookin:xlValues, matchCase:=True, After:=findafter)
  If not foundrow is nothing then
     redim preserve DayAddresses(i)
     DayAddresses(i) =foundrow.Value & "|" & foundrow.Address
     vals = Split(dayAddresses(0), "|")
     firstfound =Vals(1)
     application.findformat.clear 'someone said need to clear .find if re-using
     Set findafter =range(foundrow.Address(0,0))
     i=i+1
   end if
   loop while foundrow.address <> firstfound
end with

0 个答案:

没有答案