【问题标题】:Concatenation in VBA until an empty cell is foundVBA中的连接直到找到一个空单元格
【发布时间】:2018-11-05 01:24:41
【问题描述】:

如何创建与“,”连接的所有第一列编号,直到单元格为空。然后从下一个空单元格开始,代码应重复 n=1000 个数字。

例子:

第一列数字包括

12
34
445
565

Here there is an empty cell in the 1st column

345
4767
765

结果:

12,34,445,565

345,4767,765

【问题讨论】:

  • 我想@DisplayName 想说的是:欢迎来到董事会!我认为您正在逐步浏览该范围内的每个单元格并将这些值粘贴在一个字符串变量中。如果您可以分享您的代码并解释您在哪里以及如何遇到问题。此链接应该有助于充实您的问题:How to Ask
  • 他甚至可以用谷歌搜索解决方案 extendoffice.com/documents/excel/… 尽管我会以不同的方式来做

标签: vba excel


【解决方案1】:

正如我在评论中所说,与site 的建议相比,我会以稍微不同的方式来做这件事

Option Explicit

Sub ConcatenateCells()

Const DELIMITER = ","
Dim rg As Range
Dim col As Collection
Dim vDat As Variant
Dim i As Long

    Set rg = Range("A1:A1000")
    vDat = WorksheetFunction.Transpose(rg)
    Set col = New Collection
    Dim colInp As String

    For i = LBound(vDat) To UBound(vDat)

        If Len(vDat(i)) = 0 And Len(colInp) > 0 Then
            col.Add colInp
            colInp = ""
        Else
            If Len(colInp) = 0 Then
                colInp = vDat(i)
            Else
                colInp = colInp & DELIMITER & vDat(i)
            End If
        End If

    Next i

    ' Output the result (collection) in the immediate window
    For i = 1 To col.Count
        Debug.Print col.Item(i)
    Next i

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-07
    • 2015-10-07
    • 1970-01-01
    • 1970-01-01
    • 2018-10-14
    • 1970-01-01
    相关资源
    最近更新 更多