将单个单元格中的字符串分成几个单元格

时间:2013-02-13 20:24:20

标签: excel excel-vba parsing split concatenation vba

我有需要分成单个点的数据。我的宏将数据绘制为散点图,其中:列A作为图表的标题,列B作为X轴,列C和D作为Y轴。我需要的是当产品ID列出的数字超过1时,将数字拆分成各自的行,并保持列B,C和D对于从原始行创建的每一行相同。因此,对于第167行,我希望分别在B,C和D中分别使用包装200和100的3行(001,002,003)。我不知道从哪里开始。我试图建立一个宏但是,当我试图记录一个“查找”公式来运行数据时,我立刻被绊倒了。任何帮助将不胜感激。

A栏:001,002,003 // B栏:包装// C栏:200 // D栏:100

抱歉,我无法发布我的数据截图,论坛不会让我。如果您有任何疑问,请告诉我,我一定会经常办理入住手续。

提前致谢。

4 个答案:

答案 0 :(得分:1)

我很快就这样做了,并没有太在意效率,但这应该可以解决问题:

  Sub SplitUpVals()

  Dim i As Long
  Dim ValsToCopy As Range
  Dim MaxRows As Long
  Dim ValToSplit() As String
  Dim CurrentVal As Variant


     MaxRows = Range("A1").End(xlDown).Row

     For i = 1 To 10000000

        ValToSplit = Split(Cells(i, 1).Value, ",")
        Set ValsToCopy = Range("B" & i & ":D" & i)

        For Each CurrentVal In ValToSplit

           CurrentVal = Trim(CurrentVal)
           Cells(i, 1).Value = CurrentVal
           Range("B" & i & ":D" & i).Value = ValsToCopy.Value

           Cells(i + 1, 1).EntireRow.Insert
           i = i + 1
           MaxRows = MaxRows + 1
        Next

        Cells(i, 1).EntireRow.Delete

     If i > MaxRows Then Exit For

     Next i

  End Sub

请注意,请确保数据下方的单元格中没有数据,因为它可能会被删除。

答案 1 :(得分:0)

您需要解析A列中的数据。我会通过将字符串拆分为数组来执行此操作,然后迭代数组项以在必要时添加/插入其他行。

如果没有看到你的工作表,我可能会从这样的东西开始,它将你的单元格值从列A分割成一个数组,然后你可以迭代数组中的项目来根据需要操作工作表。 / p>

Sub TestSplit()
Dim myString as String
Dim myArray() as String
Dim cell as Range
Dim i as Long

For each cell in Range("A2",Range("A2").End(xlDown))
    myString = cell.Value 

    myArray = Split(myString, ",")  '<-- converts the comma-delimited string in to an array
    For i = lBound(myArray) to uBound(myArray)
        If i >= 1 Then
            'Add code to manipulate your worksheet, here
        End If
    Next
Next
End Sub

答案 2 :(得分:0)

这是一个更好的解决方案(现在我有更多的时间:)) - 希望这可以解决问题!

  Sub SplitUpVals()

  Dim AllVals As Variant
  Dim ArrayIndex As Integer
  Dim RowLooper As Integer


   AllVals = Range("A1").CurrentRegion
   Range("A1").CurrentRegion.Clear

   RowLooper = 1

   For ArrayIndex = 1 To UBound(AllVals, 1)
      ValToSplit = Split(AllVals(ArrayIndex, 1), ",")

        For Each CurrentVal In ValToSplit

           CurrentVal = Trim(CurrentVal)
           Cells(RowLooper, 1).Value = CurrentVal
           Cells(RowLooper, 2).Value = AllVals(ArrayIndex, 2)
           Cells(RowLooper, 3).Value = AllVals(ArrayIndex, 3)
           Cells(RowLooper, 4).Value = AllVals(ArrayIndex, 4)

           RowLooper = RowLooper + 1
         Next

   Next ArrayIndex

  End Sub

答案 3 :(得分:0)

Sub DivideData()

'这会将组合到同一行的所有代码拆分成各自独立的行,并带有各自独立的数据

Dim a, b, txt As String, e, s, x As Long, n As Long, i As Long, ii As Long
With Range("a1").CurrentRegion
    a = .Value
    txt = Join$(Application.Transpose(.Columns(1).Value))
    x = Len(txt) - Len(Replace(txt, ",", "")) + .Rows.Count
    ReDim b(1 To x * 2, 1 To UBound(a, 2))
    For i = 1 To UBound(a, 1)
        For Each e In Split(a(i, 1), ",")
            If e <> "" Then
                For Each s In Split(e, "-")
                    n = n + 1
                    For ii = 1 To UBound(a, 2)
                        b(n, ii) = a(i, ii)
                    Next
                    b(n, 1) = s
                Next
            End If
        Next
    Next
    With .Resize(n)
        .Columns(1).NumberFormat = "@"
        .Value = b
    End With
End With

End Sub