0

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

A 列:001、002、003 // B 列:包装 // C 列:200 // D 列:100

对不起,我不能发布我的数据截图,论坛不会让我。如果您有任何问题,请告诉我,我一定会经常检查。

提前致谢。

4

4 回答 4

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

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

于 2013-02-13T22:44:59.043 回答
0

您将需要解析 A 列中的数据。我会通过将字符串拆分为一个数组来做到这一点,然后遍历数组项以在必要时添加/插入其他行。

在没有看到您的工作表的情况下,我可能会从这样的事情开始,它将您的单元格值从 A 列拆分为一个数组,然后您可以遍历数组中的项目以根据需要操作工作表。

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
于 2013-02-13T22:42:15.443 回答
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
于 2013-02-14T14:30:19.213 回答
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

结束子

于 2013-02-14T15:39:32.070 回答