简体   繁体   English

使用VBA将选定的行移到excel表的顶部或底部(不包括一列)

[英]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.

相关问题 在Excel 2016中使用VBA将选定范围移动到一列上 - Move selected range over one column with VBA in Excel 2016 Excel 2013 | 尝试从一列中的列表底部移动到下一列中的顶部 - Excel 2013 | Trying to move from bottom of list in one column to top of next column Excel 2007 VBA-如何删除电子表格中的所有行(不包括一列中的行) - Excel 2007 VBA - How to delete all row in a spreadsheet excluding the rows in one column 使用 Excel 的 vba 在公式中向左移动一列 - Move one column to the left inside a formula using Excel's vba Excel / VBA-如何选择当前选定单元格所在列(相对)的顶行(绝对)? - Excel / VBA - How to select the top row (absolute) of whichever column (relative) the currently selected cell is in? Excel VBA(非常慢)如果满足条件,则将整行移动到底部 - Excel VBA (very slow) If condition is met then move entire row to the bottom 在Excel中使用vba填充行x列设计的表格 - Populating a table for a row x column design using vba in Excel 在excel vba中将选择内容移到其相应列的底部 - Move selection to their respective column's bottom in excel vba VBA Excel-“ .Find”按从上到下的顺序搜索第K列 - VBA Excel - “.Find” Search from top to bottom in order for column K Excel vba 宏在满足条件时将列的一部分向下移动一行 - Excel vba macro to move portion of column down one row when condition is met
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM