[英]Move the selected row to the top or bottom of the excel table (excluding one column) using VBA
I want to have two VBA codes the first one moves the selected cell row in column (A, C, D, E and F) to the Top and other one moves the row cells to the bottom of the excel table. 我想要两个VBA代码,第一个将行(A,C,D,E和F)中的选定单元格行移到顶部,而另一个将行单元格移到excel表的底部。
For example if I select the cell in the C3 and run the macro the cells (A3, C3, D3,E3 and F3) in a row will move to the bottom of the table. 例如,如果我在C3中选择单元格并运行宏,则一行中的单元格(A3,C3,D3,E3和F3)将移至表格的底部。
I have the following code which moves the entire row to the bottom: 我有以下代码将整个行移到底部:
Public Sub MoveToBottom()
If Selection.Row <> 1 Then
'move the whole row
Selection.EntireRow.Cut
Range("B1").End(xlDown).Offset(1, 0).EntireRow.Insert
End If
End Sub
My Data looks like 我的数据看起来像
Thanks in advance 提前致谢
This is pretty quick and dirty but should do the trick for you. 这是非常快捷和肮脏的,但是应该为您解决问题。
It first checks that the selected row: has values, is not the header, and is not already the top/bottom row (depending on the function). 它首先检查选定的行:是否具有值,不是标题,并且还不是顶部/底部行(取决于功能)。
Then it inserts a row (for top only), copies the current row to where it belongs, and deletes the "old" row. 然后,它插入一行(仅用于顶部),将当前行复制到它所属的位置,并删除“旧”行。
Option Explicit
Sub MoveToTop()
Dim rowCurrent As Long
rowCurrent = Selection.row
If WorksheetFunction.CountA(rows(rowCurrent)) >= 5 _
And rowCurrent <> 1 And rowCurrent <> 2 Then
Range("A2, C2:F2").Insert Shift:=xlDown
Range("A" & rowCurrent).copy Destination:=Range("A2")
Range("C" & rowCurrent & ":F" & rowCurrent).copy Destination:=Range("C2:F2")
Range("A" & rowCurrent & ", " & "C" & rowCurrent & ":F" & rowCurrent).Delete
End If
End Sub
Sub MoveToBottom()
Dim rowCurrent As Long
rowCurrent = Selection.row
Dim rowLast As Long
rowLast = Range("A" & rows.count).End(xlUp).row
If WorksheetFunction.CountA(rows(rowCurrent)) >= 5 _
And rowCurrent <> 1 And rowCurrent <> rowLast Then
Range("A" & rowCurrent).copy Destination:=Range("A" & rowLast + 1)
Range("C" & rowCurrent & ":F" & rowCurrent).copy Destination:=Range("C" & rowLast + 1 & ":F" & rowLast + 1)
Range("A" & rowCurrent & ", " & "C" & rowCurrent & ":F" & rowCurrent).Delete
End If
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.