嗨,我正在尝试遍历Excel中的某些行以收集信息并将其粘贴到其他工作表中。基本上,我需要遍历行并根据产品名称指定范围,因此当下一行具有新产品名称时,范围将重新开始。这将收集同一产品的所有属性,并且仅在新范围/新表的第一行中插入用逗号分隔的属性,然后从给定文件中复制变体。
我可以将所有变化从原始工作表复制到新工作表,但第一行应在一个单元格中具有所有属性(大小/颜色)。我设法遵循了在这里找到的一些代码,但是我只获取了属性数组并创建了变体。应从此信息范围创建父产品。
Sub ProductVariations()
Dim productRow As Long, variationRow As Long
Dim size As String
Dim color As String
Dim skuCounter As Integer
Dim skuChild As String
Dim sizeArray() As String
Dim colorArray() As String
Dim sizeElement As Variant
Dim colorElement As Variant
Dim productsSheet As Worksheet
Dim variationsSheet As Worksheet
productRow = 2
variationRow = 2
Set productsSheet = ActiveWorkbook.Sheets(1)
Set variationSheet = ActiveWorkbook.Sheets(2)
'loop through each row until a blank row is found
While productsSheet.Cells(productRow, 1).Value <> ""
'get the size and color values for the current row
size = productsSheet.Cells(productRow, 3)
color = productsSheet.Cells(productRow, 4)
'make sure a size and color exists then process
If Len(size) > 0 And Len(color) > 0 Then
'reset the sku counter
skuCounter = 0
'split the size and color into arrays
sizeArray = Split(size, "|")
colorArray = Split(color, "|")
'loop through each size
For Each sizeElement In sizeArray
'loop through each color
For Each colorElement In colorArray
'increment the child counter for this variation
skuCounter = skuCounter + 1
'set the appropriate cells in the variations sheet (you have some more work to do here
skuChild = productsSheet.Cells(productRow, 2).Value & "-" & skuCounter
variationSheet.Cells(variationRow, 1).Value = productsSheet.Cells(productRow, 1)
variationSheet.Cells(variationRow, 3).Value = skuChild
'increment the variation row so the next variation goes in the next row
variationRow = variationRow + 1
Next colorElement
Next sizeElement
End If
'very important increment the next row or only the first row will ever get processed
productRow = productRow + 1
Wend
End Sub
感谢您的帮助。谢谢