Excel 用VBA宏链接多个单元格

Excel 用VBA宏链接多个单元格,excel,vba,Excel,Vba,我想把100个细胞连在一起。使用excel Formula,其工作原理如下: A2&&B2&等等 我想在单元格内容之间保留一点空间,如上面的示例公式所示 是否有一个宏或函数可以轻松地为这么多的数据完成此操作?TEXTJOIN Excel 在单元格CW1: =TEXTJOIN(" ",FALSE,A1:CV1) 向下复制到单元格CW10000 VBA 选项显式 Sub tj() ' Constants Const FirstCell As String =

我想把100个细胞连在一起。使用excel Formula,其工作原理如下:

A2&&B2&
等等

我想在单元格内容之间保留一点空间,如上面的示例公式所示

是否有一个宏或函数可以轻松地为这么多的数据完成此操作?

TEXTJOIN Excel

在单元格
CW1

=TEXTJOIN(" ",FALSE,A1:CV1)
向下复制到单元格
CW10000

VBA

选项显式

Sub tj()
    ' Constants
    Const FirstCell As String = "A1"
    Const NoR As Long = 10000
    Const NoC As Long = 100
    ' Define range.
    Dim rng As Range
    Set rng = Range(FirstCell).Resize(NoR, NoC)
    With rng.Resize(, 1).Offset(, NoC)
        ' Write formulas.
        .Formula = "=TEXTJOIN("" "",FALSE," & rng.Rows(1).Address(0, 0) & ")"
        ' Write values.
         .Value = .Value
    End With
    ' Inform user.
    MsgBox "Data concatenated.", vbInformation, "Success"
End Sub

Sub tj2()

   ' Constants
    Const FirstCell As String = "A1"
    Const NoR As Long = 10000
    Const NoC As Long = 100

    ' Define range.
    Dim rng As Range
    Set rng = Range(FirstCell).Resize(NoR, NoC)

    ' Write values from range to array.
    Dim Data As Variant
    Data = rng.Value

    ' Write resulting values to first column of array.
    Dim CurrentValue As String
    Dim i As Long
    Dim j As Long
    For i = 1 To 10000
        CurrentValue = ""
        For j = 1 To 100
            CurrentValue = CurrentValue & " " & Data(i, j)
        Next j
        Data(i, 1) = Right(CurrentValue, Len(CurrentValue) - 1)
    Next i

    ' Write values from first column of array to column range.
    rng.Resize(, 1).Offset(, NoC).Value = Data

    ' Inform user.
    MsgBox "Data concatenated.", vbInformation, "Success"

End Sub

TEXTJOIN是一个选项,就像@vbasic208的答案一样,不幸的是,它仅在Excel 2019和365(以及更高版本,供未来读者使用)中可用。 如果您使用的是早期版本的excel,您可以这样做

将此自定义项放入标准VBA模块中:

Public Function MyTextJoin(rng As Range)
    Dim str As String
    Dim arr As Variant
    arr = Application.Transpose(Application.Transpose(rng.Value))
    str = Join(arr, " ")
    MyTextJoin = str
End Function

然后在工作表上这样使用它:
=MyTextJoin(A1:CV1)

非常感谢,不知何故“=TEXTJOIN(“,FALSE,A1:CV1)”这不会在值之间留出一个空格Shello horst,哦,这可能是原因:D.非常感谢horst!!仅在Office 365中可用:我将在2019年使用它。对于错误信息,很抱歉,我一定错过了。我在应答器中添加了2019,也许分隔符可以是MyTextJoin的可选字符串参数。