将多行(一列)合并到一行(最后一行),并删除以前的行

我创build了一个Excelmacros,旨在将所有选中的多行的内容合并到最后一行,如下面的代码所示:

Sub asdf() Dim rCell As Range Dim rRng As Range Dim rslt As String Dim lastCell As Range Dim i As Integer Set rRng = Range(Selection.Address) For Each rCell In rRng.Cells Debug.Print rCell.Address, rCell.Value rslt = rslt & " " & rCell.Value rslt = Trim(rslt) rCell.Value = rslt Next rCell End Sub 

问题是,如何删除除最后一行以外的所有多个(整个)行?

你可以试试这个

 Sub asdf() Dim delCell as Range Dim rCell As Range Dim rRng As Range Dim rslt As String Dim lastCell As Range Dim i As Integer Set rRng = Range(Selection.Address) For Each rCell In rRng.Cells Debug.Print rCell.Address, rCell.Value rslt = rslt & " " & rCell.Value rslt = Trim(rslt) If Not rCell.Row = rRng.Row + rRng.Rows.Count - 1 Then If delCell is Nothing then set delCell = rCell Else set delcell = union(delCell, rCell) End If End If rCell.Value = rslt Next rCell delCell.EntireRow.Delete End Sub