反转完整的Excel工作表

时间:2018-12-01 22:06:28

标签: excel vba

我想反转一个完整的图纸,这意味着第一行成为最后一行,最后一行成为第一行,依此类推(无需更改列)。

我想到的唯一方法是在从下到上的行上运行,并将每一行复制到新的工作表上。

4 个答案:

答案 0 :(得分:4)

我知道您已经有了答案;但是我总是尽量使vba尽可能简单。 只需根据需要更改ThisWorkbook.Worksheets("Sheet1")即可满足您的需求。

Dim ws As Worksheet, lRow As Long, i As Long

Set ws = ThisWorkbook.Worksheets("Sheet1")
lRow = ws.Cells(Rows.Count, 1).End(xlUp).Row

    For i = 2 To lRow
        ws.Cells(i, 1).EntireRow.Cut
        ws.Cells(1, 1).EntireRow.Insert Shift:=xlDown
    Next i

答案 1 :(得分:2)

好吧,我很无聊,决定快点写出来。

Private Sub this()
    Dim pickUp As Variant
    Dim newArr() As String
    Dim rowC As Long, colC As Long, i As Long, j As Long, z As Long

    rowC = ThisWorkbook.Worksheets("Sheet1").UsedRange.Rows.Count
    z = rowC
    colC = ThisWorkbook.Worksheets("Sheet1").UsedRange.Columns.Count
    ReDim newArr(1 To rowC, 1 To colC)
    pickUp = ThisWorkbook.Worksheets("Sheet1").UsedRange

    For i = LBound(pickUp, 1) To UBound(pickUp, 1)
        For j = LBound(pickUp, 2) To UBound(pickUp, 2)
            newArr(rowC, j) = pickUp(i, j)
        Next j
        rowC = rowC - 1
    Next i

    Dim ws As Worksheet
    Set ws = ThisWorkbook.Worksheets("Sheet2")
    With ws
        ws.Range(.Cells(1, 1), .Cells(z, colC)).Value2 = newArr
    End With
End Sub

答案 2 :(得分:0)

乏味必须具有传染性。

Sub FlipTheScript()
    Dim data As Variant, result As Variant

    data = ThisWorkbook.Worksheets("Sheet1").UsedRange.Value
    ReDim result(1 To UBound(data), 1 To UBound(data, 2))

    Dim r As Long, c As Long

    For r = 1 To UBound(data)
        For c = 1 To UBound(data, 2)
            result(UBound(data) - r + 1, c) = data(r, c)
        Next
    Next

    ThisWorkbook.Worksheets("Sheet1").UsedRange.Value = result
End Sub

答案 3 :(得分:0)

  1. 插入虚拟列
  2. 按号码填写
  3. 降序排列
  4. 删除编号的列

    Dim ws As Worksheet
    Dim i As Long
    
    Set ws = ActiveWorkbook.Worksheets("Sheet1")
    
    Columns("A:A").Insert Shift:=xlToRight
    For i = 1 To ws.UsedRange.Rows.Count
        ws.Cells(i, 1) = i
    Next i
    
    With ws.Sort
        .SortFields.Clear
        .SortFields.Add Key:=Range("A1"), Order:=xlDescending
        .SetRange ws.UsedRange
        .Apply
    End With
    
    Columns("A:A").Delete