使用命令button反转/反转没有设置范围的列

我知道这个问题已经被问到,但我似乎无法使我find为我工作的东西。

我只想从列A开始的所有数据,并从第2行到第J列的任何数据结束可能是颠倒顺序(反向的数据)

我偶然发现了下面的代码,但是它冻结了,我不想做出select。

Private Sub CommandButton2_Click() Dim vTop As Variant Dim vEnd As Variant Dim iStart As Integer Dim iEnd As Integer Application.ScreenUpdating = False iStart = 1 iEnd = Selection.Columns.Count Do While iStart < iEnd vTop = Selection.Columns(iStart) vEnd = Selection.Columns(iEnd) Selection.Columns(iEnd) = vTop Selection.Columns(iStart) = vEnd iStart = iStart + 1 iEnd = iEnd - 1 Loop Application.ScreenUpdating = True End Sub 

要清楚,我想使最后一行成为第一行,最后一行成为第一行。 这是一个连续的数据块。 干杯

之前

代码的另一个版本 – 看看是否有效。

 Private Sub CommandButton2_Click() Dim v(), i As Long, j As Long, r As Range Application.ScreenUpdating = False Application.Calculation = xlCalculationManual With Range("A1").CurrentRegion Set r = .Offset(1).Resize(.Rows.Count - 1) End With ReDim v(1 To r.Rows.Count, 1 To r.Columns.Count) For i = 1 To r.Rows.Count For j = 1 To r.Columns.Count v(i, j) = r(r.Rows.Count - i + 1, j) Next j Next i r = v Application.Calculation = xlCalculationAutomatic Application.ScreenUpdating = True End Sub 

下面的代码将每列中的数据复制到列号20 – 当前列索引,并在For循环结束时删除列A:J中的原始数据。

 Option Explicit Private Sub CommandButton2_Click() Dim LastRow As Long Dim Col As Long Dim ColStart As Long, ColEnd As Long Application.ScreenUpdating = False ' Column A ColStart = 1 ' Column J ColEnd = 10 ' Selection.Columns.Count ' modify "Sheet1" to your sheet's name With Sheets("Sheet1") For Col = ColStart To ColEnd ' find last row with data for current column LastRow = .Cells(.Rows.Count, Col).End(xlUp).Row ' copy in reverse order to column 20 to 11 ' copy current column to column 20-current column index .Range(Cells(2, Col), Cells(LastRow, Col)).Copy .Range(Cells(2, 20 - Col), Cells(LastRow, 20 - Col)) Next Col End With ' delete original data in column A:J With Sheets("Sheet1") .Columns("A:J").EntireColumn.Delete End With Application.ScreenUpdating = True End Sub 

像这样? 这假定来自A2(当前区域)的连续的数据块,如果有更多的数据,则会延伸到J以外,但是可以被限制

 Private Sub CommandButton2_Click() Dim v, i As Long, r As Range Application.ScreenUpdating = False With Range("A1").CurrentRegion Set r = .Offset(1).Resize(.Rows.Count - 1) End With v = r For i = 1 To r.Rows.Count r.Rows(i).Cells = Application.Index(v, r.Rows.Count - i + 1, 0) Next i Application.ScreenUpdating = True End Sub 
Interesting Posts