0

Excel ワークシートの行の順序を逆にするマクロを作成しようとしましたが、残念ながら失敗しました。どうやって始めたらいいのかもわかりません。それを行う方法についての助けやヒントをいただければ幸いです。

4

3 に答える 3

4

行を選択して、次のマクロを実行します。

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 をコメントアウトしてください。

于 2013-07-29T15:52:54.680 に答える
1

入力パラメーターとして行番号を持つように、Wallys コードを少し単純化して変更しました。

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

トビ

于 2016-05-20T20:18:59.883 に答える