For Loop超出参数VBA

我做了以下的循环,每个单元格下去,直到它到达与另一列中最后使用的单元格的水平的单元格。 但是,它远远超出了最后使用的单元格。 它工作正常,但只需要很长的时间运行,特别是当它有3万行通过! 有没有人有任何想法在这一个?

Dim i As Long lMaxRows = Cells(Rows.Count, "K").End(xlUp).Row For i = 1 To lMaxRows Range("D" & lMaxRows + 1).Select ActiveCell.FormulaR1C1 = "0:00:00" lMaxRows = Cells(Rows.Count, "E").End(xlUp).Row Range("E" & lMaxRows + 1).Select ActiveCell.FormulaR1C1 = "1" Next i 

定义父级工作表,避免select并一次填充所有的单元格。

 With Worksheets("Sheet1") With .Range(.Cells(1, "K"), .Cells(.Rows.Count, "K").End(xlUp)) .Offset(0, -7) = "0:00:00" .Offset(0, -6) = 1 End With End With 

我认为这是由于lMaxRows使用和循环。

这是否工作?

 Sub t2() Dim lMaxRows&, xMaxRows& Dim i As Long lMaxRows = Cells(Rows.Count, "K").End(xlUp).Row i = 1 Do While i <= lMaxRows Range("D" & lMaxRows + 1).FormulaR1C1 = "0:00:00" xMaxRows = Cells(Rows.Count, "E").End(xlUp).Row Range("E" & xMaxRows + 1).FormulaR1C1 = "1" i = i + 1 Loop End Sub 

没有一些样本数据,很难知道你想要做什么或期望输出什么。