我正在运行一个带有CSV导入套装插件的Woocommerce商店。我需要首先上传包含父产品的列表,然后再列出包含产品变体的列表。创建第二个列表是相当费力的,所以我正在寻找一种尽可能自动生成此列表的方法。
以下是简短的简化父产品列表的样子:
包含父产品的实际列表将包含更多产品特征。
这就是产品变化的结果应如下所示:
如您所见,可以看到产品变体(子)隐含了母产品的所有产品特征,只有一个尺寸和颜色变量。如果自动生成子sku也会很好。
我希望在不同的工作表中生成输出数据(产品变体)(与父产品列表分开)。
有没有人有任何想法如何在Excel中执行此操作?我不是Excel向导,所以我希望你能给我一些关于如何实现你的解决方案的指导。
答案 0 :(得分:1)
这里的逻辑是读取每一行,直到找到一个空行,然后分割颜色和大小。然后为颜色创建外部循环,为尺寸创建内部循环(这将处理所有变体)。您还需要一个变量来保存子sku计数器,并在对产品进行变化时重置它。以下内容将帮助您入门:
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