移动行/范围但不高于某一行

时间:2018-01-24 21:07:46

标签: excel vba row

我有以下子项:

Sub Rowsup1()

With Selection.EntireRow
    .Cut
    .Offset(.Rows.count - 2).Insert
    .Select
End With

End Sub

我想阻止所选行在第6行上方移动。我并不确定最佳方法。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

此代码将删除Selection所在的整行,然后将其插入一行。一旦您的数据在第6行中,它就会停止这样做。它不会循环播放,因此,如果您正在查找的内容(例如,单击按钮一次,并且该行继续移动,直到它击中第6行,你需要做一些修改。

Sub Rowsup1()
Dim myRow As Integer

 Application.ScreenUpdating = False

   With Selection
'   find the currently selected row
    myRow = .Row

'   While myRow doesn't = 6, cut and paste the entire _
    row one row higher than its current location
    If myRow = 6 Then
      'Do nothing
    Else
      .EntireRow.Cut
      Range("A" & myRow - 1).Insert
      .Select
    End If
   End With

   With Application
    .CutCopyMode = False
    .ScreenUpdating = True
   End With

End Sub

我建议只将其更改为If myRow <= 6 Then,这样如果您已经 - 由于某种原因 - 已经通过第6行,则不会发生任何事情。

相关问题