在使用 Visual Basic 執行簡單的 while 回圈時,我遇到了 Excel 崩潰的問題。這個想法是我想選擇并復制表中屬于第一列中定義的某個“批次”的所有行
Sub Copy5Cells()
lColumn = Range("C7").End(xlToRight).Column
StartRow = ActiveCell.Row
Batch = Cells(StartRow, 1).Value
ControlBatch = Batch
MsgBox ControlBatch
Do Until (ControlBatch <> Batch)
nextRow = StartRow 1
ControlBatch = Cells(nextRow, 1).Value
Loop
num_rows = (nextRow - StartRow)
Range(ActiveCell, ActiveCell.Offset(num_rows, (lColumn - 2))).Copy
End Sub

uj5u.com熱心網友回復:
每次 DO UNTIL 回圈回圈并再次開始時,它都會將 nextRow 設定為 StartRow 1。但是,StartRow 始終具有相同的值,因此 nextRow 也將始終具有相同的值。
在 LOOP 陳述句之前,添加一行以增加 StartRow:
Do Until (ControlBatch <> Batch)
nextRow = StartRow 1
ControlBatch = Cells(nextRow, 1).Value
StartRow = StartRow 1
Loop
或者,在回圈開始之前設定初始 nextRow 并在回圈之前增加它:
nextRow = StartRow 1
Do Until (ControlBatch <> Batch)
ControlBatch = Cells(nextRow, 1).Value
nextRow = nextRow 1
Loop
num_rows = (nextRow - StartRow) - 1
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/361271.html
上一篇:在一個Excel作業簿中拆分具有相同名稱范圍的作業表-ExcelVBA
下一篇:更改單個單元格范圍中的單元格值
