我正在尝试使用逗号分隔值创建基于多个单元格的记录。
拿这个:
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
非常感谢任何帮助。
答案 0 :(得分: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
起始数据如下所示:
结果放在这样的新栏目中: