1

我正在尝试基于具有逗号分隔值的多个单元格创建记录。

拿着这个:

    Handle      |     Title    |   Color    |   Size     |     Price 
                |              |            |            |  (0-04/06-08)
---------------------------------------------------------------------------
Jovani-JVN86957 |     Jovani   |  Black,    | 0, 02,     |   $199 (if size > 06  then +15)
                     JVN86957  | Red, White | 04, 06, 08 |     

我要做的是让颜色和大小列生成新记录,同时包括句柄和价格(如果可能但没有必要根据大小值更改价格,即:大小 0-4 = 199 美元和大小6-8 = 219 美元)

转向这个:

Handle                Title         Color    Size  Price(0 04/06 08 +$15)

Jovani-JVN86957   Jovani-JVN86957   Black    0        $199
Jovani-JVN86957                     Black    2        $199
Jovani-JVN86957                     Black    4        $199
Jovani-JVN86957                     Black    6        $199 +$15 = $214
Jovani-JVN86957                     Black    8        $199 +$15 = $214
Jovani-JVN86957                     Red      0        $199
Jovani-JVN86957                     Red      2        $199
Jovani-JVN86957                     Red      4        $199
Jovani-JVN86957                     Red      6        $199 +$15 = $214
Jovani-JVN86957                     Red      8        $199 +$15 = $214
Jovani-JVN86957                     White    0        $199
Jovani-JVN86957                     White    2        $199
Jovani-JVN86957                     White    4        $199
Jovani-JVN86957                     White    6        $199 +$15 = $214
Jovani-JVN86957                     White    8        $199 +$15 = $214

任何帮助将非常感激。

4

1 回答 1

0

这是一种使用多个循环的方法,它更像是一种蛮力方法。

Sub SplitEntries()
    Dim rng As Range
    Set rng = Range("A2:A5")

    'Copy over header
    Range("G1:K1").Value = Range("A1:E1").Value

    Dim sizeSplit As Variant
    Dim colorSplit As Variant
    Dim rowCntr As Integer: rowCntr = 2

    'Loop thru range
    For Each cell In rng
        Range("H" & rowCntr).Value = cell.Offset(0, 1).Value

        'Get all the colors
        colorSplit = Split(cell.Offset(0, 2), ",")

        'Loop through each color
        For i = LBound(colorSplit) To UBound(colorSplit)
            'Get all the sizes
            sizeSplit = Split(cell.Offset(0, 3), ",")

            'Loop through each size
            For j = LBound(sizeSplit) To UBound(sizeSplit)
                Range("G" & rowCntr).Value = cell.Value
                Range("I" & rowCntr).Value = Trim(colorSplit(i))
                Range("J" & rowCntr).Value = Trim(sizeSplit(j))
                'Range("K" & rowCntr).Value = cell.Offset(0, 4).Value

                If Trim(sizeSplit(j)) < 6 Then
                   Range("K" & rowCntr).Value = cell.Offset(0, 4).Value
                Else
                   Range("K" & rowCntr).Value = cell.Offset(0, 4).Value + 15
                End If

                rowCntr = rowCntr + 1
            Next j
        Next i
    Next cell
End Sub

起始数据如下所示:

在此处输入图像描述

结果被放入这样的新列中:

在此处输入图像描述

于 2013-12-19T04:32:21.637 回答