Excel 将多行(一列)合并为一行(最后一行),并删除以前的行
我创建了一个Excel宏,用于将所有选定多行的内容一列合并到最后一行,代码如下:Excel 将多行(一列)合并为一行(最后一行),并删除以前的行,excel,vba,Excel,Vba,我创建了一个Excel宏,用于将所有选定多行的内容一列合并到最后一行,代码如下: 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
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