繁体   English   中英

计数器的值未在 For 循环中更新

[英]Value of counter is not updating in a For Loop

我在 excel 文件的 B 列中有数据。 我做了一个循环,如果 B 单元格中的值大于特定 (len1) 值,则代码将单元格 (Value-Len1) 值放在行末尾的新单元格中。

每次添加行时,我都会将计数器递增为 lastrow = lastrow+1。 现在问题来了。 最初在输入文件中我有 122 组数据。 但是当 For 循环完成时,lastrow 的值变为 160,但循环在 122 处退出。为什么? 任何帮助将不胜感激。

For i = 1 To lastrow Step 1
    If Range("B" & i).Value > len1 Then
        Range("A" & lastrow + 1).Value = Range("A" & i).Value
        Range("B" & lastrow + 1).Value = Range("B" & i).Value - len1
        Range("B" & i).Value = len1
        lastrow = lastrow + 1
    End If
Next

要获得您想要的行为,您需要一个while循环(或do循环)

i = 1
While i <= lastrow
    If Range("B" & i).Value > len1 Then
        lastrow = lastrow + 1
        Range("A" & lastrow).Value = Range("A" & i).Value
        Range("B" & lastrow).Value = Range("B" & i).Value - len1
        Range("B" & i).Value = len1
    End If
    i = i + 1
Wend

我用下面的sub测试了它:

Sub LoopBeyondLastRow()
    Dim i  As Long, lastrow As Long, len1 As Long
    
    len1 = 10
    
    With ThisWorkbook.Sheets("Sheet1")
        lastrow = .Cells(Rows.Count, "B").End(xlUp).Row
        
        i = 1
        While i <= lastrow
            If .Range("B" & i).Value > len1 Then
                lastrow = lastrow + 1
                .Range("A" & lastrow).Value = .Range("A" & i).Value
                .Range("B" & lastrow).Value = .Range("B" & i).Value - len1
                .Range("B" & i).Value = len1
            End If
            i = i + 1
        Wend
    End With
End Sub

请注意以下事项:

  • 在循环内部,我先增加lastrow ,然后在以下 2 个语句中使用它(以减少加法操作的数量)
  • 在我的测试代码中,我添加了With ThisWorkbook.Sheets("Sheet1")以完全限定所有范围。 不这样做是许多有时很难查明的错误的根源。 一个人应该养成一个习惯,即永远不要在没有一点的情况下编写RangeCells . 在他们面前。

请测试下一个代码:

Sub TestLoopAddedRowsInclusive()
  '..... your code defining LastRow and len1
  Dim lastRInit As Long
  lastRInit = LastRow
  For i = 1 To Rows.count
    If Range("B" & i).Value = "" And i >= lastRInit Then Exit For
    If Range("B" & i).Value > len1 Then
        Range("A" & LastRow + 1).Value = Range("A" & i).Value
        Range("B" & LastRow + 1).Value = Range("B" & i).Value - len1
        Range("B" & i).Value = len1
        LastRow = LastRow + 1
    End If
  Next
End Sub

一种更快的方法是将范围值导出到数组,然后进行比较。 将最终的 output 存储到临时数组中并将其写回工作表。

如果您想遵循您的方法,那么这就是您正在尝试的吗? 我已经对代码进行了注释,因此您理解它应该没有问题。 如果要重新检查在行尾添加的数据,基本上需要 2 个循环。

Option Explicit

Sub Sample()
    Dim ws As Worksheet
    Dim ComparisionValue As Long
    Dim countOfMatchedValues As Long
    Dim lRow As Long
    Dim i As Long
    Dim outputRow As Long
    Dim rng As Range
    
    '~~> Change this to the relevant sheet
    Set ws = Sheet1
    
    '~~> Change this to the relevant
    '~~> comparision value
    ComparisionValue = 122
    
    With ws
        '~~> Start an indefinite loop
        Do
            '~~> Find last row
            lRow = .Range("A" & .Rows.Count).End(xlUp).Row
            '~~> Fix the output row for the new data
            outputRow = lRow + 1
            
            '~~> Check if there are any matches for your condition
            countOfMatchedValues = Application.WorksheetFunction.CountIf( _
            .Range("B1:B" & lRow), ">" & ComparisionValue)
            
            '~~> If not then exit loop
            If countOfMatchedValues = 0 Then Exit Do
            
            '~~> Do your stuff
            For i = 1 To lRow
                If .Range("B" & i).Value > ComparisionValue Then
                    .Range("A" & outputRow).Value = .Range("A" & i).Value
                    .Range("B" & outputRow).Value = .Range("B" & i).Value - ComparisionValue
                    .Range("B" & i).Value = ComparisionValue
                    outputRow = outputRow + 1
                End If
            Next i
        Loop
    End With
End Sub

在行动

在此处输入图像描述

for循环使用预定义的迭代次数。 对于未知数量的迭代,您需要使用while循环。

您的代码在解释时使用lastRow的值,并且永远不会再次更新。

这类似于:

lastRow = 1
Debug.Print lastRow
lastRow = lastRow + 1
Debug.Print lastRow

你会看见:

1
2

不是

2
2

因为一旦执行了第一个Debug语句,更改lastRow的值就不会再影响这个特定的 output 了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM