如何拆分超过活动单元格

时间:2016-07-22 19:49:32

标签: excel vba excel-vba excel-2013

我试图让一个单元格根据公式返回的值进行拆分。 它不会让我上传照片所以我会尽力解释。

以下是拆分单元格的代码:

Sub splitText()
Dim splitVals As Variant, totalVals As Long, I As Integer
splitVals = Split(ActiveCell.Value, " ")
totalVals = UBound(splitVals)
range(Cells(ActiveCell.Row, ActiveCell.Column + 1), Cells(ActiveCell.Row,ActiveCell.Column + 1 + totalVals)).Value = splitVals
End Sub

我想知道是否有办法让我的程序设置,以便它将分割D列中的所有单元格,而不仅仅是活动单元格。

2 个答案:

答案 0 :(得分:0)

在这里,试试这个:

    Sub splitText()

    Dim r As Range
    Dim splitVals As Variant, totalVals As Long, I As Integer

    For Each r In Range("D:D").Cells
        If r.Value <> "" Then
            splitVals = Split(r.Value, " ")
            totalVals = UBound(splitVals)
            Range(Cells(r.Row, r.Column + 1), Cells(r.Row, r.Column + 1 + totalVals)).Value = splitVals
        End If
    Next r


    End Sub

答案 1 :(得分:0)

Sub splitText()
   Dim splitVals As Variant, totalVals As Long
   Dim N as single
   splitVals = Split(ActiveCell.Value, " ")
   totalVals = UBound(splitVals)
   For N = 0 to totalVals
      ActiveCell.offset(0, 1 + N) = splitVals(N)
      ActiveCell.offset(1, 0).select
   Next N
End Sub

此子程序仅用于拆分单个单元格的文本,这是您的活动单元格。 如果需要拆分多个单元格,则需要为每个单元格循环此函数。