使用我当前的代码退出循环

时间:2014-08-15 14:18:30

标签: excel vba excel-vba excel-2010

感谢所有帮助过我的人,但是我再次陷入困境。这与我自己已经找到答案的另一个问题有关。发布链接以获取参考:Previous Question我想知道是否有人可以帮我弄清楚如何退出循环。一切都按预期工作,因为我创造了一个无限循环。一旦它到达最后一行或记录,我需要它退出For ... Next循环。谢谢。代码附后。

Sub DuplicateSystems()
'
' Created for use with the Vulnerability Remediation Asset Manager Site Baseline Summary
'

'Declared Variables
Dim lastRow As Long, currentRow As Long

Application.ScreenUpdating = False

'Determines the Total number of Records
Dim recCount As Integer
With Sheets(1)
    recCount = .Range("A" & .Rows.count).End(xlUp).Row - 2
End With

'Assigns a value to the lastRow variable
lastRow = recCount + 2

'Loops through the records, hides any system that has not been scanned in 30 days
For currentRow = 3 To lastRow + 1
    If Cells(currentRow, 8) > 30 Then

        'Assign the current system to a string value
        Dim sys As String, rng As Range
        sys = Cells(currentRow, 4).value

        'Figures out the Start Row for the current system
        Dim nRow As Long, sRow As Long, eRow As Long
        For nRow = 3 To lastRow
            If Range("D" & nRow).value = sys Then
                sRow = nRow
                Exit For
            End If
        Next nRow

        'Figures out the End Row for the current system
        For nRow = sRow To lastRow
            If Range("D" & nRow) <> sys Then
                eRow = nRow - 1
                Exit For
            End If
        Next nRow

        'Selects the all rows for that system
        Set rng = Range("D" & sRow & ":D" & eRow)
        rng.Select

        'Displays a message box with the range address
        MsgBox rng.Address

        'Hides the selection
        rng.EntireRow.Hidden = True

        'Changes the currentRow to the end of the current system
        If eRow = Range("D" & lastRow) Then
            Exit For
        Else: currentRow = eRow
        End If

    'Shows all other systems
    Else: Cells(currentRow, 4).EntireRow.Hidden = False
    End If

Next currentRow

'Counts the remaining the systems that are visible
Dim count As Integer
count = Sheets("sheet1").Range("D3:D" & lastRow).Columns(4).SpecialCells(xlCellTypeVisible).count

'Displays a message box with a number of the remaining systems
MsgBox count

Application.ScreenUpdating = True
End Sub

2 个答案:

答案 0 :(得分:0)

我可能是错的,但我相信你的currentRow永远是你的sRow所以sRow是不必要的。所以你可以删除

For nRow = 3 To lastRow
  If Range("D" & nRow).value = sys Then
    sRow = nRow
    Exit For
  End If
Next nRow

然后将sRow替换为currentRow

答案 1 :(得分:0)

我想我知道你的问题所在。

如果有一个大于30的值,你最后一次循环就会循环,因为你的代码部分在哪里;

    For nRow = sRow To lastRow
        If Range("D" & nRow) <> sys Then
            eRow = nRow - 1
            Exit For
        End If
    Next nRow

对于你下去的最后一个循环,从来没有时间是Range(&#34; D&#34;&amp; nRow)&lt;&gt; SYS。因为它们始终是相同的,因为它是你的最后一个系统。因为您永远不会满足IF声明,所以您永远不会重新定义您的eRow。将代码更改为

    For nRow = sRow To lastRow + 1
        If Range("D" & nRow) <> sys Then
            eRow = nRow - 1
            Exit For
        End If
    Next nRow 

应该解决这个问题。

如果这有帮助,请告诉我。