Excelmacros将一行移动到底部

美好的一天,

我正在尝试创build一个macros,根据标准将一行移动到工作表的底部。 到目前为止,我所能做的是将行复制到底部,但这会为我创build一个重复行,实际上我只需要将它移动。

'Moving column "Grand Total" to bottom With Wbk4.Sheets("TEST") FinalRow = Cells(Rows.Count, 1).End(xlUp).Row 'Loop through each row For x = 2 To FinalRow 'Decide if to copy based on column A ThisValue = Cells(x, 1).Value If ThisValue = "Grand Total" Then Cells(x, 1).Resize(1, 33).Copy lrow = .Range("A" & .Rows.Count).End(xlUp).Row .Range("A" & lrow + 1, "Z" & lrow + 1).PasteSpecial xlPasteAll End If Next x End With 

谢谢

由于您没有提供任何示例数据,因此很难推荐自定义sorting,但是右侧的临时帮助列可以快速将所有总计行移动到最下面。

 With Wbk4.Sheets("TEST") With .Cells(1, 1).CurrentRegion .Columns(.Columns.Count).Offset(1, 1).Resize(.Rows.Count - 1, 1).Formula = "=--(A2=""Grand Total"")" End With With .Cells(1, 1).CurrentRegion 'reestablish current region with new helper column .Cells.Sort Key1:=.Columns(.Columns.Count), Order1:=xlAscending, _ Orientation:=xlTopToBottom, Header:=xlYes .Columns(.Columns.Count).Cells.ClearContents End With End With 

如果你想添加额外的sorting顺序,还有两个额外的sorting键(最多三个不加倍)。

尝试Cells(x, 1).EntireRow.DeleteCells(x, 1).Resize(1, 33).Delete End If

谢谢Jeeped,它工作正常! 我在尝试你的代码之前使用了另一种方法,它也可以工作! 如果有人在将来寻找代码引用,我将在下面发布它以供参考

 'Moving column B to bottom With Wbk4.Sheets("test") FinalRow = .Cells(rows.Count, 1).End(xlUp).Row 'Loop through each row For x = 2 To FinalRow 'Decide if to copy based on column A ThisValue = .Cells(x, 1).Value If ThisValue = "Grand Total" Then .Cells(x, 1).Resize(1, 33).Select Selection.Cut lRow = .Range("A" & .rows.Count).End(xlUp).Row .Range("A" & lRow + 1, "Z" & lRow + 1).Select ActiveSheet.Paste End If Next x End With 'Delete Blank Rows Dim i As Long With Wbk4.Sheets("test") For i = .Range("A" & rows.Count).End(xlUp).Row To 1 Step -1 If .Range("A" & i) = "" Then .Range("A" & i).EntireRow.Delete End If Next i End With