2016-11-10 79 views
0

我在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 
+0

您是否嘗試過我的解決方案? – Nick

回答

1

只是

.Cells(RowFirst + 1, 1).Resize(Selection.Rows.Count - 1, 1).ClearContents 

NOTE 「結尾」 之前添加此。在我的電腦上測試過並工作過

+0

@ YowE3K我現在編輯了我的答案。是的,以前的代碼未經測試無法正常工作。 – Nick