2013-08-28 52 views
1

在先前發佈的答案,Combine multiple cells into one in excel with macro?分隔值,下面的宏是provided..which的方式的偉大工程!宏多個單元格合併成一個,用逗號

然而,我需要的單元值用逗號分隔。我試圖在引號之間插入逗號「,」但這不起作用。有人可以引導我,所以得到的單元格會用逗號分隔值嗎?謝謝!

Sub JoinCells() 

Set xJoinRange = Application.InputBox(prompt:="Highlight source cells to merge",  Type:=8) 
xSource = 0 
xSource = xJoinRange.Rows.Count 
xType = "rows" 
If xSource = 1 Then 
xSource = xJoinRange.Columns.Count 
xType = "columns" 
End If 
Set xDestination = Application.InputBox(prompt:="Highlight destination cell", Type:=8) 
If xType = "rows" Then 
temp = xJoinRange.Rows(1).Value 
For i = 2 To xSource 
    temp = temp & " " & xJoinRange.Rows(i).Value 
Next i 
Else 
temp = xJoinRange.Columns(1).Value 
For i = 2 To xSource 
    temp = temp & " " & xJoinRange.Columns(i).Value 
Next i 
End If 

xDestination.Value = temp 

End Sub 

回答

1

看一看這裏: http://www.excelforum.com/tips-and-tutorials/860240-concatall-udf-by-tigeravatar.html

的第一篇文章介紹瞭如何使用它,以及第8帖子是最新版本。

[編輯] 或者,只是改變的& " " &這兩種情況是& "," &

+0

我試過改變這個&「」&是&「,」& 但它不起作用。將值拆分爲2,這樣代替12345,23189我得到123,452,318。有任何想法嗎? –

+0

啊,它們是數字,Excel將它轉換爲完整的數字,並帶有標準的逗號分隔。您可以格式xDestination與'xDestination.NumberFormat文本=「@」'或者你可以在你得到的數值前,像這樣加一個撇號:'xDestination.Value =「'」&temp' – tigeravatar

+0

此外,我提供的鏈接包含將根據需要執行的UDF。你可能想嘗試一下。 – tigeravatar

0

這裏是我用來做這個功能。它只適用於單個色譜柱,但可以根據需要輕鬆調整以適用於多個色譜柱。

Function CommaList(DataRange As Range, Optional Seperator As String = ",", Optional Quotes As Boolean = True) As String 
    Dim iCellNum As Integer 
    Dim sTemp As String 

    If DataRange.Columns.Count > 1 Then 
     CommaList = "Select a Single Column" 
     Exit Function 
    End If 

    For iCellNum = 1 To DataRange.Cells.Count 
     If Quotes Then 
      sTemp = sTemp + "'" + DataRange.Cells(iCellNum, 1) + "'" 
     Else 
      sTemp = sTemp + DataRange.Cells(iCellNum, 1) 
     End If 
     If iCellNum < DataRange.Cells.Count Then 
      sTemp = sTemp + Seperator 
     End If 
    Next iCellNum 

    CommaList = sTemp 

End Function 
相關問題