VBA列的递归循环

时间:2018-03-03 21:59:58

标签: excel vba recursion

如果我可以递归循环遍历For循环中的排序列表,有人可以解释一下吗?

我正在遍历一个列,一旦我找到完全匹配(比如说 EALOLES 字符串),那么我想继续循环,直到没有更多的匹配。 Data example

For i = 2 to UsedRange.Rows.Count
  If (Cells(i, 12).Value = "EALOLES") Then
    ' Start an inner loop until EALOLES ends, increment i++
    ' Perform actions appropriate to EALOLES case
    Exit For
  End If
next i

这对于内循环来说都很好,但我只是想知道是否可以通过递归函数实现这一点以及它看起来如何?从我学习递归的例子中,我想象从工作簿的结尾循环到开头。

注意,我并不是说这是一个更好的解决方案,也不是内循环,但我只是非常好奇。

4 个答案:

答案 0 :(得分:1)

你的问题基本上是这个递归的候选人,答案是否定的。在这种情况下,使用内循环进行迭代是更好的解决方案。

阅读文章:Recursion and Iteration以了解何时使用每个文章。

答案 1 :(得分:1)

假设您的数据已排序,您可以利用

Dim nOccurrences As Long
Dim cell As Range
With Intersect(ActiveSheet.UsedRange, Columns(12))
    nOccurrences = WorksheetFunction.CountIf(.Cells, "EALOLES")
    If nOccurrences > 0 Then
        For Each cell in .Resize(nOccurrences).Offset(.Find(What:= "EALOLES", LookIn:=xlValues, LookAt:=xlWhole, After:=.Cells(.Rows.Count)).Row-1)
              ‘Do your things
        Next
    End If
End With

答案 2 :(得分:0)

这不是一种在排序列表中返回字符串的开始和停止位置的有效方法,但作为一种智力练习应该这样做。

dim i as long, j as long

For i = 2 to UsedRange.Rows.Count
  If (Cells(i, 12).Value = "EALOLES") Then
    for j=i to UsedRange.Rows.Count
      If (Cells(j+1, 12).Value <> "EALOLES") Then
        exit for
      end if
    next j
    Exit For
  End If
next i

debug.print "start: " & i
debug.print "end: " & j

答案 3 :(得分:0)

我对同一主题的看法略有不同

定义要循环的范围。查看该值是否存在于该范围内。如果是,则从第一个匹配开始,并保持循环循环范围,直到单元格值与指定的目标字符串不同。

Option Explicit

Sub StopAtEnd()

    Dim wb As Workbook
    Dim ws As Worksheet
    Dim endRow As Long

    Set wb = ThisWorkbook
    Set ws = wb.Worksheets("Sheet5")             'change as needed

    endRow = ws.Cells(ws.Rows.Count, "L").End(xlUp).Row

    Dim loopRange As Range

    Set loopRange = ws.Range("L1:L" & endRow) 'Change start row as required

    Dim currentCell As Range
    Dim targetString As String
    Dim startRow As Long

    targetString = "EALOLES"

    On Error GoTo Errhand

    startRow = Application.Match(targetString, loopRange, 0)

    Do Until ws.Range("L" & startRow) <> targetString
        Debug.Print ws.Range("L" & startRow).Address
        startRow = startRow + 1
    Loop

    Exit Sub

Errhand:

    MsgBox "Target string not found"

End Sub

喊出@DisplayName,他指出这可以改为:

Option Explicit

Sub StopAtEnd()

    Dim wb As Workbook
    Dim ws As Worksheet
    Dim endRow As Long

    Set wb = ThisWorkbook
    Set ws = wb.Worksheets("Sheet1")             'change as needed

    endRow = ws.Cells(ws.Rows.Count, "L").End(xlUp).Row

    Dim loopRange As Range

    Set loopRange = ws.Range("L1:L" & endRow) 'Change start row as required

    Dim currentCell As Range
    Dim targetString As String
    Dim startRow As Variant

    targetString = "EALOLES"

    startRow = Application.Match(targetString, loopRange, 0)

    If IsError(startRow) Then

        MsgBox "Target string not found"

    Else

        Do Until ws.Range("L" & startRow) <> targetString

            Debug.Print ws.Range("L" & startRow).Address
            startRow = startRow + 1

        Loop

   End If

End Sub