我需要将多行选择中的多个单元格连接成一个带有换行符/回车符的单个字符串单元格
e.g。选择图片:
连接后应该如下所示:
我的代码连接到一个单元格但没有添加换行符:
Sub concat_3()
Dim row As Range
Dim cell As Range
Dim txt As String
For Each row In Selection
For Each cell In row.Cells
txt = txt & cell.Value
Next cell
txt = txt & vbCrLf
Next row
Selection.ClearContents
txt = Left(txt, Len(txt) - 2)
Selection(1).Value = txt
End Sub
答案 0 :(得分:1)
我用For循环编辑你的代码(不是For Each),它对我有用。尝试。
Sub concat_3()
Dim c As Range
Dim txt As String
Dim i As Integer
Dim j As Integer
Set c = Selection
txt = ""
For i = 1 To c.Rows.Count
For j = 1 To c.Columns.Count
txt = txt & c(i, j).Value
Next j
txt = txt & vbCrLf
Next i
txt = Left(txt, Len(txt) - 2)
'to output in Sheet1 A1
Sheets("Sheet1").Range("A1") = txt
End Sub
答案 1 :(得分:0)