直到循环 – 删除范围实际上不会停止VBA
您好,我有代码做直到循环,删除我定义的范围。 但是,一旦它达到我指定的“直到”参数,它将继续删除我的范围。 为什么它继续删除? 而且我怎样才能让它实际上停止删除一次RowCount = 155?
Dim RowCount As Long Dim Dados As Worksheet Set Dados = Sheets("Dados") RowCount = Dados.Cells(Rows.Count, "BL").End(xlUp).Row Do Range("BL5:BP5").Delete Loop Until RowCount = 155
想象一下G和H是你的BL:BP区域:
看来你要求的是把这些东西下面的数据上移到一定的数量来得到这个:
在你的例子中,你可以这样做:
Dim RowCount As Long Dim Dados As Worksheet Set Dados = Sheets("Dados") RowCount = Dados.Cells(Rows.Count, "BL").End(xlUp).Row Dados.Range("BL5:BP" & RowCount - 155).Delete xlShiftUp
我最终得到它与这个代码。
Sub Newlastmonths() Dim RowCount As Long Dim Dados As Worksheet Dim LeaveRows As Long Dim x As Long Dim y As Long Set Dados = Sheets("Dados") RowCount = Dados.Cells(Rows.Count, "BL").End(xlUp).Row LeaveRows = RowCount - 156 x = 5 y = x + LeaveRows For i = x To y Range("BL" & i & ":BP" & i).Delete Next i End Sub
你可以稍微改变一下你的循环
Dim RowCount As Long Dim Dados As Worksheet Dim iRows As Integer Set Dados = Sheets("Dados") RowCount = Dados.Cells(Rows.Count, "BL").End(xlUp).Row For iRows = 1 to RowCount Range("BL5:BP5").Delete Next iRows