使用Excel宏连接字符串

时间:2016-04-01 09:51:17

标签: excel excel-vba macros vba

我有一个场景,我需要连接1列的行。为此,我试图创建一个宏。但是,当我使用公式时,它抛出了应用程序定义的错误。以下是代码

Sub ABCS()
Dim U
Dim str As String
Set myArrayList = CreateObject("System.Collections.ArrayList")
Set ws = ThisWorkbook.Worksheets("Sheet2")
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

lastrow = ws.Cells(65536, 1).End(xlUp).Row
MsgBox lastrow
For i = 1 To lastrow

str = Cells(i, 1).Value

Cells(2, 2).Formula = "=CONCATENATE(" & str & ")"

Next i



End Sub

1 个答案:

答案 0 :(得分:1)

希望这是你在寻找......

Sub ABCS()
    Dim U
    Dim str As String
    Set myArrayList = CreateObject("System.Collections.ArrayList")
    Set ws = ThisWorkbook.Worksheets("Sheet2")
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    lastrow = ws.Cells(65536, 1).End(xlUp).Row
    MsgBox lastrow
    For i = 1 To lastrow
        str = Cells(i, 1).Value
        Final = Final & str
    Next i
    Cells(2, 2) = Final
End Sub

另一种进行缠身的方法如下。在这里,我们可以直接使用concetenation Range调用特定单元格(参见下图)。

Public Function concaetenateme(r As Range) As String
    Dim str As String
    For i = 1 To r.Cells.Count
        str = r.Cells(i)
        Final = Final & str
    Next i
    concaetenateme = Final
End Function

enter image description here