回答
作爲用戶函數採取range
Public Function ClarkeyCat(ByRef rng As Range) As Variant
Dim c As Range
Dim ans As Variant
For Each c In rng
If (c.Value <> "") Then
ans = IIf(ans = "", "", ans & ",") & c.Value
End If
Next
ClarkeyCat = ans
End Function
改變Variant
類型,如果你需要(以string
,最有可能的)。
使用這樣的:
這可能是最優雅的解決方案。 – 2014-09-02 17:30:49
您可以隨時使用Visual Basic For Applications(VBA)。它是Office的微軟語言。以下是您可能要查找的示例,但請嘗試使用Google Machine瞭解有關VBA的更多信息以及如何將此代碼輸入到電子表格中。
Sub ConcatColumns()
Do While ActiveCell <> "" 'Loops until the active cell is blank.
'The "&" must have a space on both sides or it will be
'treated as a variable type of long integer.
ActiveCell.Offset(0, 1).FormulaR1C1 = _
ActiveCell.Offset(0, -1) & " " & ActiveCell.Offset(0, 0)
ActiveCell.Offset(1, 0).Select
Loop
End Sub
這並沒有解決逗號需要放置在單元格值之間。 – 2014-09-02 17:37:22
它只是將一行中的最後兩個單元格連接在一行之前。 – 2014-09-02 17:43:02
我會用VBA這一點。對於每一列你會想要類似的東西(假設值在第1行)
myString = ""
for i = 1 to 40
if i <> 40 then
myString = myString & Cells(1, i) & ", "
else:
myString = myString & Cells(1, i)
end if
next i
myString將會有你的連接字符串的內容。
當串聯一系列單個行或列,你可以使用Application.Transpose
避免範圍單杆做循環
此UDF有三個參數
- 1D範圍(可以是列或行)
- A n可選分隔符(如果沒有entrey,則使用
,
) - 可選項用於指定範圍是否爲一行(對於範圍,請輸入
TRUE
- 進一步認爲我將更新UDF以自動檢測範圍是row
ORcolumn
爲主)
注意的是,在其他的答案
IIF
評估兩個方面TRUE
和FALSE
參數爲VBA
不[短路]( http://en.wikipedia.org/wiki/Short-circuit_evaluation)。所以IFF
可以是昂貴的內部循環 串聯時加入了長串短串的組合輸出,而不是長着短
- ,則長有短又
代碼
Function ConCat(rng1 As Range, Optional StrDelim As String, Optional bRow As Boolean) As String
Dim x
If StrDelim = vbNullString Then StrDelim = ","
x = Application.Transpose(rng1)
If bRow Then x = Application.Transpose(x)
ConCat = Join(x, StrDelim)
End Function
在下面
- 式(
D1
)的例子是=concat(A1:C1,",",TRUE)
- 在
E1
公式是=concat(E3:E5,", ")
讓我發表我的功能了。我也遇到過這個問題。
當我嘗試連接日期,錯誤和空白單元格時,通常會出現我的問題。
所以我嘗試使用下面覆蓋的大多數人:
Function CONCATPLUS(ref_value As Range, Optional delimiter As Variant) As String
Dim cel As Range
Dim refFormat As String, myvalue As String
If ref_value.Cells.Count = 1 Then CONCATPLUS = CVErr(xlErrNA): Exit Function
If IsMissing(delimiter) Then delimiter = " "
For Each cel In ref_value
refFormat = cel.NumberFormat
Select Case TypeName(cel.Value)
Case "Empty": myvalue = vbNullString
Case "Date": myvalue = Format(cel, refFormat)
Case "Double"
Select Case True
Case refFormat = "General": myvalue = cel
Case InStr(refFormat, "?/?") > 0: myvalue = cel.Text
Case Else: myvalue = Format(cel, refFormat)
End Select
Case "Error"
Select Case True
Case cel = CVErr(xlErrDiv0): myvalue = "#DIV/0!"
Case cel = CVErr(xlErrNA): myvalue = "#N/A"
Case cel = CVErr(xlErrName): myvalue = "#NAME?"
Case cel = CVErr(xlErrNull): myvalue = "#NULL!"
Case cel = CVErr(xlErrNum): myvalue = "#NUM!"
Case cel = CVErr(xlErrRef): myvalue = "#REF!"
Case cel = CVErr(xlErrValue): myvalue = "#VALUE!"
Case Else: myvalue = "#Error"
End Select
Case "Currency": myvalue = cel.Text
Case Else: myvalue = cel
End Select
If Len(myvalue) <> 0 Then
If CONCATPLUS = "" Then
CONCATPLUS = myvalue
Else
CONCATPLUS = CONCATPLUS & delimiter & myvalue
End If
End If
Next
End Function
截至目前,我還沒有遇到一個單元格輸入這個功能不能連接。
隨意適應您的需求或心中的內容。 HTH。
- 1. Excel列連接
- 2. 許多MySQL連接
- 3. 用連接數和許多列排序
- 4. 許多睡眠httpd連接
- 5. Excel:將一列分成許多列
- 6. Excel列表VBA連接
- 7. 許多一對多映射與額外的列連接表
- 8. 使用宏多列連接
- 9. 列在Excel一列如何將多個連接(我想配方)
- 10. 連接多個列
- 11. 文本列許多Excel工作簿
- 12. Excel基於其他列連接列
- 13. 如何連接許多numpy數組?
- 14. 無法連接許多numpy文件?
- 15. 許多客戶端連接Ruby
- 16. Processlist中的許多Binlog_dump連接?
- 17. MySQL的:在許多連接表
- 18. 處置不工作,許多死連接
- 19. 許多連接與大數據查詢
- 20. ADAM允許多少個併發連接?
- 21. 如何允許多個連接到Dancer2
- 22. JBoss連接池創建與MySQL的許多連接
- 23. 許多SQL連接到LINQ的外連接
- 24. 將許多行連接起來並連接成一個
- 25. C#Excel連接
- 26. Excel宏連接
- 27. MySQL的許多列匹配到一個與左連接
- 28. Excel宏 - 太多的連接行
- 29. 在MS Excel中連接兩列
- 30. Excel表單連接不覆蓋列
http://www.get-digital-help.com/2011/02/09/concatenate-a-cell-range-without-vba-in-excel/ – 2014-09-02 16:56:58