Excel 2010macros

嗨,大家好我是新来的macros和VBA。 我需要创build一个macros,将多行中的多个单元格连接到一个单一的string单元格换行/回车

例如select图像:

在这里输入图像说明

串联后应该看起来像这样:

连接成一个单元格:

在这里输入图像说明

我当前的代码并没有连接到一个单元格,但不添加换行符:

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 

任何帮助,我将不胜感激。

我用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 

这是另一个去:

之前:

在这里输入图像说明

一些代码:

 Sub fhuscvc() Dim s As String, i As Long, a As Range i = 0 s = "" For Each a In Selection s = s & " " & a.Value i = i + 1 If i = 2 Then i = 0 s = s & vbCrLf End If Next a Selection.Clear Selection(1).Value = s End Sub 

之后:

在这里输入图像说明

分隔每一对物品的固定空格块不是很漂亮….太差了TAB字符在单元格内不起作用。