Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 将多行(一列)合并为一行(最后一行),并删除以前的行_Excel_Vba - Fatal编程技术网

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

我创建了一个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
        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