如何获取由两个空格分隔的一个单元格中的单元格范围

How to get range of cells in one cell separated by two blank spaces

我需要一个 vba 代码来将由两个 space 分隔的单元格值范围连接到另一个单元格中。
例如。

一个
a2 b
a3c
a4空白
a5空白
a6d
a7 e
a8f

那么单元格 B 的值将是

b1 a,b,c
b2d,e,f

此代码连接单个范围并连接 space。我希望 space 成为定界符并在 space 之后连接范围并循环遍历它。

Sub demo()
    'i = 2
    'if cells(i,2).value
    Dim lastRow As Long
    lastRow = ActiveSheet.Range("a" & Rows.Count).End(xlUp).Row
    MsgBox (lastRow)
    For i = 2 To lastRow
        'Do Until Cells(i, 2).End(xlDown).Row = ""
        If Cells(i, 3).Value = "" Then
            Sheet2.Cells(i, 1).Value = Cells(i, 1) & " " & Cells(i, 2)
        Else
            Sheet2.Cells(i, 1).Value = Cells(i, 1) & " " & _
                Cells(i, 2) & "(" & Cells(i, 3) & ")"
        End If
        ' i = i + 1
    Next i
End Sub     

你可以试试这个。这解决了你的第一个问题。我不明白你是在第二部分提出另一个问题,还是试图解释第一部分。无论如何,sub 将 single 空白视为要处理的范围的一部分。图片中的示例输出。

只需更改输入范围(下图"A1:A30")。它将结果放在与输入数据相邻的列中。

Public Sub spaced_out()

Dim rr, Ain As Range
Dim AB As Object

Dim rr_out_count As Integer

Set Ain = ActiveSheet.Range("A1:A30")
Set AB = CreateObject("scripting.dictionary")

For Each rr In Ain
    If (rr.Value <> "") Then
            AB.Add rr.Row(), rr.Value
    ElseIf (rr.Value = "" And rr.Offset(1, 0).Value = "") Then
            rr_out_count = rr_out_count + 1
            Ain.Offset(0, 1).Cells(rr_out_count, 1).Value = Join(AB.items(), ",")
            AB.RemoveAll
    End If
Next


End Sub