2010-01-18 33 views

回答

4

根據the thread you are citing,我猜你希望返回單元格中所有值的連接,將所有值解釋爲字符串?

對於這一點,你可以使用VBA宏,看起來像這樣:

Function ConcatinateAllCellValuesInRange(sourceRange As Excel.Range) As String 
    Dim finalValue As String 

    Dim cell As Excel.Range 

    For Each cell In sourceRange.Cells 
     finalValue = finalValue + CStr(cell.Value) 
    Next cell 

    ConcatinateAllCellValuesInRange = finalValue 
End Function 

舉個例子,你可以這樣調用它:

Sub MyMacro() 
    MsgBox ConcatinateAllCellValuesInRange([A1:C3]) 
End Sub 

難道這就是你要找的人?

邁克

4

嘗試下面的宏,因爲它沒有做任何錯誤檢查等,但作品不是很優雅。將宏指定到一個按鈕,單擊單元格,單擊宏按鈕,突出顯示所需的(源)範圍內使用鼠標進行合併(將範圍在對話框中輸入框自動填充),單擊確定,突出目的地細胞(將自動填充在下一個對話框輸入框)單擊OK,所有細胞將被用一個空格字符到目的細胞,其可以是在原來的源範圍合併)。由您決定手動刪除多餘的單元格。具有行和列但不包含塊的工作。

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 
0

只需添加到Mike的解決方案,如果你想從一個變量而不是確定的範圍內讓你的範圍(我曾與語法麻煩):

Sub MyMacro() 

dim myVar As Range 

    MsgBox ConcatinateAllCellValuesInRange(myVar) 

End Sub