Excel 在值的第一个实例上插入行
我试图在第一个实例中插入一行,其中y列中的值大于60。只有一排 我做了一个循环,为60以上的任何内容插入多行,但我不需要这个 很难改变这一点 这是我得到的Excel 在值的第一个实例上插入行,excel,vba,if-statement,ifs,Excel,Vba,If Statement,Ifs,我试图在第一个实例中插入一行,其中y列中的值大于60。只有一排 我做了一个循环,为60以上的任何内容插入多行,但我不需要这个 很难改变这一点 这是我得到的 Dim Col As Variant Dim BlankRows As Long Dim LastRow As Long Dim i As Long Dim StartRow As Long Col = "Y" StartRow = 1
Dim Col As Variant
Dim BlankRows As Long
Dim LastRow As Long
Dim i As Long
Dim StartRow As Long
Col = "Y"
StartRow = 1
BlankRows = 1
LastRow = Cells(Rows.Count, Col).End(xlUp).Row
Application.ScreenUpdating = False
With ActiveSheet
For i = LastRow To StartRow + 1 Step -1
If .Cells(i, Col) > 60 Then
.Cells(i, Col).EntireRow.Insert shift:=xlDown
End If
Next i
End With
Application.ScreenUpdating = True
End Sub
完成
true
后,您可以使用GoTo
跳过以下迭代
Dim Col As Variant
Dim BlankRows As Long
Dim LastRow As Long
Dim i As Long
Dim StartRow As Long
Col = "Y"
StartRow = 1
BlankRows = 1
LastRow = Cells(Rows.Count, Col).End(xlUp).Row
Application.ScreenUpdating = False
With ActiveSheet
For i = LastRow To StartRow + 1 Step -1
If .Cells(i, Col) > 60 Then
.Cells(i, Col).EntireRow.Insert shift:=xlDown
Goto: Close
End If
Next i
End With
Close
Application.ScreenUpdating = True
End Sub
完成
true
后,您可以使用GoTo
跳过以下迭代
Dim Col As Variant
Dim BlankRows As Long
Dim LastRow As Long
Dim i As Long
Dim StartRow As Long
Col = "Y"
StartRow = 1
BlankRows = 1
LastRow = Cells(Rows.Count, Col).End(xlUp).Row
Application.ScreenUpdating = False
With ActiveSheet
For i = LastRow To StartRow + 1 Step -1
If .Cells(i, Col) > 60 Then
.Cells(i, Col).EntireRow.Insert shift:=xlDown
Goto: Close
End If
Next i
End With
Close
Application.ScreenUpdating = True
End Sub
您可以为此使用Do循环(2个搜索方向变体):
您可以为此使用Do循环(2个搜索方向变体):
或者只需在if子句中退出即可。Hi@ceci我认为使用
Exit
的问题在于,它会在有机会重新打开屏幕更新之前关闭子例程,而GoTo:
允许OP仍然执行执行此操作的部分。否则,这将是一个很好的替代方案,而且包含起来会简单得多。不,“退出for”将只退出for循环,然后继续执行代码的其余部分。对不起,@ceci我错过了退出for
的部分。这也会起作用,谢谢你的选择,我为我的疏忽道歉。或者你的if子句中的一个退出也会起作用。嗨@ceci我认为使用退出
的问题是它会在有机会重新打开屏幕更新之前关闭子例程,而GoTo:
允许OP仍然执行执行此操作的部分。否则,这将是一个很好的替代方案,而且包含起来会简单得多。不,“退出for”将只退出for循环,然后继续执行代码的其余部分。对不起,@ceci我错过了退出for
的部分。这也行,谢谢你的选择,为我的疏忽道歉。首先
-你的意思是当你向下扫描还是向上扫描时?要插入的新行位于>60行的下方或上方。如果缩进保持正确对齐,读取代码会容易得多(例如,for
和相应的Next
具有相同的缩进,循环中的行会进一步缩进).第一个实例
-您是指向下扫描工作表还是向上扫描?要插入到>60行下方或上方的新行。如果缩进保持正确对齐,读取代码会容易得多(例如,for
和相应的Next
具有相同的缩进,循环中的行进一步缩进)。
Sub InsertRowSearchUp()
Dim out As Boolean, cl As Range
Set cl = ActiveSheet.Range("Y" & Rows.Count).End(xlUp) 'start cell
Do Until out 'initially out=False
If cl > 60 Then
cl.EntireRow.Insert
out = True
Else
If cl.Row = 1 Then
out = True
Else
Set cl = cl.Offset(-1) 'move UP to the previous row
End If
End If
Loop
End Sub