在范围内分发单元格的内容

时间:2018-04-12 12:20:38

标签: excel vba excel-vba excel-formula

我如何根据定义的因素在指定范围内分发excel中单元格的内容?

E.g。单元格A1包含1,2,3,4,5,6,7,8,9 期望的结果是将该系列分布在范围A1:A9

或更复杂的示例:A1包含apple, orange; 34; 67, "pod" 期望的结果是在B1:F1

范围内扩展由不同标点符号分隔的内容

感谢。

3 个答案:

答案 0 :(得分:3)

使用数据 - >文本到列执行此操作:

http://www.excel-easy.com/examples/text-to-columns.html

答案 1 :(得分:1)

在VBA中

Option Explicit

Public Sub SpreadListsInColA()

    Dim c As Range, itms As Variant

    With Sheet1.UsedRange.Columns("A")
        Application.ScreenUpdating = False
        For Each c In .Cells
            If Not IsError(c) Then
                If Len(c.Value2) > 0 And InStr(c.Value2, ",") > 0 Then

                    itms = Split(c.Value2, ",")

                    c.Offset(, 1).Resize(, UBound(itms) + 1) = itms

                End If
            End If
        Next
        Application.ScreenUpdating = True
    End With
End Sub

答案 2 :(得分:0)

这个UDF将几乎处理第一个场景。由于它是一个函数,原始文本将保留在单元格A1中,这将分布在您输入公式的单元格中。

A2:A10A2:I2中输入此数组公式: = Transpose_Ext(A1,“,”)

Public Function Transpose_Ext(Source As Range, Delim As String) As Variant

    With Application.Caller
        If Source.Cells.Count = 1 Then
            If .Rows.Count = 1 Then
                Transpose_Ext = Array(Split(Source, Delim))
            ElseIf .Rows.Count > 1 And .Columns.Count = 1 Then
                Transpose_Ext = Application.WorksheetFunction.Transpose(Array(Split(Source, Delim)))
            End If
        End If
    End With

End Function  

要使用多个分隔符,我会在函数中添加一个paramarray作为存储分隔符数组的最后一个参数,然后在每个分隔符处分割源值。

编辑:但是,如前所述,文本到列和Transpose函数也可以完成这项工作。