首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何逆转行的顺序

如何逆转行的顺序
EN

Stack Overflow用户
提问于 2013-07-29 14:10:04
回答 3查看 5.9K关注 0票数 0

我试图在Excel工作表中编写一个宏,该宏将反转行的顺序,但不幸的是,我失败了。我甚至都不知道怎么开始。我将非常感谢任何帮助或提示如何做到这一点。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-07-29 15:52:54

选择您的行并运行以下宏:

代码语言:javascript
复制
Sub ReverseList()
    Dim firstRowNum, lastRowNum, thisRowNum, lowerRowNum, length, count As Integer
    Dim showStr As String
    Dim thisCell, lowerCell As Range
    With Selection
        firstRowNum = .Cells(1).Row
        lastRowNum = .Cells(.Cells.count).Row
    End With
    showStr = "Going to reverse rows " & firstRowNum & " through " & lastRowNum
    MsgBox showStr

    showStr = ""
    count = 0
    length = (lastRowNum - firstRowNum) / 2
    For thisRowNum = firstRowNum To firstRowNum + length Step 1
        count = count + 1
        lowerRowNum = (lastRowNum - count) + 1
        Set thisCell = Cells(thisRowNum, 1)
        If thisRowNum <> lowerRowNum Then
            thisCell.Select
            ActiveCell.EntireRow.Cut
            Cells(lowerRowNum, 1).EntireRow.Select
            Selection.Insert
            ActiveCell.EntireRow.Cut
            Cells(thisRowNum, 1).Select
            Selection.Insert
        End If
        showStr = showStr & "Row " & thisRowNum & " swapped with " & lowerRowNum & vbNewLine
    Next
    MsgBox showStr
End Sub

如果您不喜欢通知,请注释掉MsgBox。

票数 4
EN

Stack Overflow用户

发布于 2016-05-20 20:18:59

我对Wallys代码做了一些简化,并对其进行了修改,以便将行号作为输入参数:

代码语言:javascript
复制
Sub ReverseList(firstRowNum As Integer, lastRowNum As Integer)

  Dim upperRowNum, lowerRowNum, length As Integer

  length = (lastRowNum - firstRowNum - 2) / 2
  For upperRowNum = firstRowNum To firstRowNum + length Step 1
    lowerRowNum = lastRowNum - upperRowNum + firstRowNum

    Cells(upperRowNum, 1).EntireRow.Cut
    Cells(lowerRowNum, 1).EntireRow.Insert
    Cells(lowerRowNum, 1).EntireRow.Cut
    Cells(upperRowNum, 1).EntireRow.Insert
  Next
End Sub

托比

票数 1
EN

Stack Overflow用户

发布于 2021-09-22 18:25:14

我用Range参数扩展了Tobag的答案,并使所有参数都是可选的:

代码语言:javascript
复制
Sub ReverseRows(Optional rng As range = Nothing, Optional firstRowNum As Long = 1, Optional lastRowNum As Long = -1)

    Dim i, firstRowIndex, lastRowIndex As Integer
    
    ' Set default values for dynamic parameters
    If rng Is Nothing Then Set rng = ActiveSheet.UsedRange
    If lastRowNum = -1 Then lastRowNum = rng.Rows.Count
    
    If firstRowNum <> 1 Then
        ' On each loop, cut the last row and insert it before row 1, 2, 3, and so on
        lastRowIndex = lastRowNum
        For i = firstRowNum To lastRowNum - 1 Step 1
            firstRowIndex = i
            rng.Rows(lastRowIndex).EntireRow.Cut
            rng.Rows(firstRowIndex).EntireRow.Insert
        Next
    Else
        ' Same as above, except handle different Insert behavior.
        ' When inserting to row 1, the insertion goes above/outside rng,
        ' thus the confusingly different indices.
        firstRowIndex = firstRowNum
        For i = firstRowNum To lastRowNum - 1 Step 1
            lastRowIndex = lastRowNum - i + 1
            rng.Rows(lastRowIndex).EntireRow.Cut
            rng.Rows(firstRowIndex).EntireRow.Insert
        Next
    End If
  
End Sub
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17926269

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档