连接两个选定的单元格并删除

时间:2016-11-10 08:06:32

标签: excel-vba macros vba excel

我在A列中有数据,包含1000行数据。我需要连接任何两个选定的单元格。例如,用户选择的单元格A10& A11在单元格A10中连接。在此之后我需要清除细胞A11的内容。

下面的代码可以很好地进行连接,并将数据放入A10中。但它不清楚A11细胞数据的内容。我需要清除A11细胞内容。

非常感谢任何帮助。

Option Explicit
Sub MergeStems()

  Dim ColFirst As Long
  Dim ColLast As Long
  Dim JoinedValue As String
  Dim RowCrnt As Long
  Dim RowFirst As Long
  Dim RowLast As Long

    RowFirst = Selection.Row 
    RowLast = RowFirst + Selection.Rows.Count - 1

    ColFirst = Selection.Column
    ColLast = ColFirst + Selection.Columns.Count - 1

    If ColFirst <> 1 Or ColLast <> 1 Then
      Call MsgBox("Please select a range within column ""A""", vbOKOnly)
      Exit Sub
    End If

    With Worksheets("Sheet1")      ' Worksheet of your choice.
      JoinedValue = .Cells(RowFirst, "A").Value
      For RowCrnt = RowFirst + 1 To RowLast
        JoinedValue = JoinedValue & " " & .Cells(RowCrnt, "A").Value
      Next
      .Cells(RowFirst, "A").Value = JoinedValue
    End With

  End Sub

1 个答案:

答案 0 :(得分:1)

在&#34;结束之前使用&#34;

添加此内容
.Cells(RowFirst + 1, 1).Resize(Selection.Rows.Count - 1, 1).ClearContents

注意:不要错过开头的点

编辑:立即尝试。在我的电脑上测试并且工作