VBA列的递归循环

问题描述 投票:0回答:4

任何人都可以解释我是否可以递归循环遍历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

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

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

excel vba recursion
4个回答
1
投票

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

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


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

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

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
© www.soinside.com 2019 - 2024. All rights reserved.