回答
作为用户函数采取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. 打开Excel与许多列
- 5. 许多TcpListener传入连接
- 6. 许多睡眠httpd连接
- 7. Excel:将一列分成许多列
- 8. Excel列表VBA连接
- 9. 连接太多由于许多睡眠连接导致错误
- 10. 许多一对多映射与额外的列连接表
- 11. 使用宏多列连接
- 12. 列在Excel一列如何将多个连接(我想配方)
- 13. 连接多个列
- 14. 文本列许多Excel工作簿
- 15. Excel基于其他列连接列
- 16. 处置不工作,许多死连接
- 17. C#模拟器“许多连接”
- 18. 许多客户端连接Ruby
- 19. Pentaho的创建许多休眠连接
- 20. 连接到许多TOR出口节点
- 21. 左连接 - 许多结果返回
- 22. MySQL的:在许多连接表
- 23. SQL到LINQ与许多连接
- 24. 连接Python中的许多数组
- 25. MySql的例外许多连接
- 26. PHP Adodb许多睡眠连接
- 27. Processlist中的许多Binlog_dump连接?
- 28. 无法连接许多numpy文件?
- 29. 如何允许多个连接到Dancer2
- 30. 如何连接许多numpy数组?
http://www.get-digital-help.com/2011/02/09/concatenate-a-cell-range-without-vba-in-excel/ – 2014-09-02 16:56:58