VBA定义每个部分的macros(string)的开始和结束?

我的表Excel的例子

你好,我怎样才能定义一个基于string的间隔,并循环到它? 例如定义从1开始到2结束,然后移动到另一个间隔? 例如,在每个时间间隔中search名称“xx”,并用msgbox显示单元格的地址。

我做了下面的代码,问题是,我不知道如何使间隔..有人可以帮我吗? 谢谢。

Sub search_for_names() lastligne = ThisWorkbook.Sheets("students").Cells(Rows.Count, 1).End(xlUp).Row For i = 1 To lastligne Set rnginformation = Cells(i, 1) Set rngaction = Cells(i, 2) If rnginformation = "1" And rngaction = "start" Then MsgBox "beginning of interval" For k = 1 To rnginformation = "2" And rngaction = "end" 'define the end of interval MsgBox "x" Set actionAnalyse = ThisWorkbook.Sheets("students").Cells(k, 2).Find(xx, LookIn:=xlValues) firstAddress = rngaction.Address MsgBox firstAddress Next k End If Next i End Sub 

当你发现你的起始行设置一个variables等于我lStart = i 。 然后,当你发现最后一行build立从起始行到我.Range(.Cells(lStart, 2), .Cells(i, 2)) 。 因为你实际上需要lStart和.Range(.Cells(lStart + 1, 2), .Cells(i -1, 2))行更有效。

 Sub search_for_names() Dim i As Long, lastligne As Long, lStart As Long Dim actionAnalyse As Range, rSearch As Range With Sheets("students") lastligne = .Cells(Rows.Count, 1).End(xlUp).Row For i = 1 To lastligne If .Cells(i, 1) = "1" And .Cells(i, 2) = "start" Then lStart = i If .Cells(i, 1) = "2" And .Cells(i, 2) = "end" Then Set rSearch = .Range(.Cells(lStart, 2), .Cells(i, 2)) Set actionAnalyse = rSearch.Find("xx", LookIn:=xlValues) If Not actionAnalyse Is Nothing Then MsgBox actionAnalyse.Address End If End If Next i End With End Sub