Vba 如何颠倒行的顺序
我试图编写一个宏,它可以颠倒Excel工作表中的行顺序,但不幸的是,我失败了。我甚至不知道如何开始。如果您能提供帮助或提示,我将不胜感激。选择您的行并运行此宏:Vba 如何颠倒行的顺序,vba,excel,Vba,Excel,我试图编写一个宏,它可以颠倒Excel工作表中的行顺序,但不幸的是,我失败了。我甚至不知道如何开始。如果您能提供帮助或提示,我将不胜感激。选择您的行并运行此宏: Sub ReverseList() Dim firstRowNum, lastRowNum, thisRowNum, lowerRowNum, length, count As Integer Dim showStr As String Dim thisCell, lowerCell As Range W
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
子反向列表()
Dim firstRowNum、lastRowNum、thisRowNum、lowerRowNum、长度,计为整数
作为字符串的Dim showStr
将此单元格调暗,将单元格调低为范围
有选择
firstRowNum=.Cells(1.Row)
lastRowNum=.Cells(.Cells.count).Row
以
showStr=“要反转行”&firstRowNum&“通过”&lastRowNum
msgboxshowstr
showStr=“”
计数=0
长度=(lastRowNum-firstRowNum)/2
对于thisRowNum=firstRowNum到firstRowNum+长度步长1
计数=计数+1
lowerRowNum=(lastRowNum-count)+1
设置thisCell=单元格(thisRowNum,1)
如果这个Rownum lowerRowNum那么
此单元格。选择
ActiveCell.EntireRow.Cut
单元格(lowerRowNum,1)。EntireRow.Select
选择.插入
ActiveCell.EntireRow.Cut
单元格(thisRowNum,1)。选择
选择.插入
如果结束
showStr=showStr&“行”&thisRowNum&“交换为”&lowerRowNum&vbNewLine
下一个
msgboxshowstr
端接头
如果不喜欢通知,请注释掉MsgBox。选择行并运行此宏:
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
子反向列表()
Dim firstRowNum、lastRowNum、thisRowNum、lowerRowNum、长度,计为整数
作为字符串的Dim showStr
将此单元格调暗,将单元格调低为范围
有选择
firstRowNum=.Cells(1.Row)
lastRowNum=.Cells(.Cells.count).Row
以
showStr=“要反转行”&firstRowNum&“通过”&lastRowNum
msgboxshowstr
showStr=“”
计数=0
长度=(lastRowNum-firstRowNum)/2
对于thisRowNum=firstRowNum到firstRowNum+长度步长1
计数=计数+1
lowerRowNum=(lastRowNum-count)+1
设置thisCell=单元格(thisRowNum,1)
如果这个Rownum lowerRowNum那么
此单元格。选择
ActiveCell.EntireRow.Cut
单元格(lowerRowNum,1)。EntireRow.Select
选择.插入
ActiveCell.EntireRow.Cut
单元格(thisRowNum,1)。选择
选择.插入
如果结束
showStr=showStr&“行”&thisRowNum&“交换为”&lowerRowNum&vbNewLine
下一个
msgboxshowstr
端接头
如果您不喜欢通知,请将MsgBox注释掉。我稍微简化了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
Tobi我稍微简化了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
Tobi插入一列,用1、2、3、4等填充,直到要反转的行的末尾。然后选择数据,包括刚才添加的列,并按相反顺序(从大到小)排序。如果需要多次执行,请使用宏记录器查看代码显示失败代码..步骤1:在从行迭代时将行添加到数组变量。计数为1。第二步:在工作表上写出数组。插入一列,用1、2、3、4等填充,直到要反转的行的末尾。然后选择数据,包括刚才添加的列,并按相反顺序(从大到小)排序。如果需要多次执行,请使用宏记录器查看代码显示失败代码..步骤1:在从行迭代时将行添加到数组变量。计数为1。步骤2:在工作表上写出数组。