[英]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")
以完全限定所有范圍。 不這樣做是許多有時很難查明的錯誤的根源。 一個人應該養成一個習慣,即永遠不要在沒有一點的情況下編寫Range
或Cells
.
在他們面前。請測試下一個代碼:
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.