使用Excel/VB根据父产品列表创建产品变体列表

使用Excel/VB根据父产品列表创建产品变体列表,excel,woocommerce,vba,Excel,Woocommerce,Vba,我正在经营一家Woocommerce商店,里面有产品CSV导入套装插件。我需要首先上传一个与父产品列表,第二个与产品的变化列表。创建第二个列表需要大量劳动,因此我正在寻找一种尽可能多地自动生成此列表的方法 下面是一个简短、简化的母产品列表: 包含父产品的实际列表将包含更多的产品特征 产品变化的结果应该是这样的: 正如您所看到的,可以看到产品变体(子)继承了父产品的所有产品特性,并且只有一个尺寸和颜色变量。如果子sku也能自动生成,那就太好了 我希望在不同的工作表中生成输出数据(产品变体)(与

我正在经营一家Woocommerce商店,里面有产品CSV导入套装插件。我需要首先上传一个与父产品列表,第二个与产品的变化列表。创建第二个列表需要大量劳动,因此我正在寻找一种尽可能多地自动生成此列表的方法

下面是一个简短、简化的母产品列表:

包含父产品的实际列表将包含更多的产品特征

产品变化的结果应该是这样的:

正如您所看到的,可以看到产品变体(子)继承了父产品的所有产品特性,并且只有一个尺寸和颜色变量。如果子sku也能自动生成,那就太好了

我希望在不同的工作表中生成输出数据(产品变体)(与父产品列表分开)


有人想过如何在Excel中实现这一点吗?我不是Excel向导,因此我希望您能就如何实现您的解决方案给我一些指导。

这里的逻辑是读取每一行,直到找到一个空行,然后拆分颜色和大小。然后为颜色创建外循环,为大小创建内循环(这将处理所有变化)。您还需要一个变量来保存子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
子产品变体()
Dim productRow尽可能长,variationRow尽可能长
像字符串一样变暗
像字符串一样暗淡的颜色
将计数器设置为整数
把孩子当作绳子
Dim sizeArray()作为字符串
Dim colorArray()作为字符串
Dim sizeElement作为变体
作为变体的暗淡颜色元素
将产品表设置为工作表
将变量调整为工作表
productRow=2
变量行=2
设置productsSheet=ActiveWorkbook.Sheets(1)
Set variationSheet=ActiveWorkbook.Sheets(2)
'循环遍历每一行,直到找到一个空白行
而productsSheet.Cells(productRow,1).Value“”
'获取当前行的大小和颜色值
大小=productsSheet.Cells(productRow,3)
颜色=productsSheet.Cells(productRow,4)
'确保存在尺寸和颜色,然后处理
如果Len(尺寸)>0且Len(颜色)>0,则
'重置sku计数器
计数器=0
'将大小和颜色拆分为数组
SizerRay=拆分(大小为“|”)
colorArray=Split(颜色为“|”)
'循环通过每个大小
对于SizeRay中的每个sizeElement
'循环每种颜色
对于colorArray中的每个colorElement
'增加此变量的子计数器
skuCounter=skuCounter+1
'在变体表中设置适当的单元格(此处还有一些工作要做
skuChild=productsSheet.Cells(productRow,2).Value&“-”和skuCounter
variationSheet.Cells(variationRow,1).Value=productsSheet.Cells(productRow,1)
variationSheet.Cells(variationRow,3).Value=skuChild
'增加变体行,使下一个变体进入下一行
variationRow=variationRow+1
下一个颜色元素
下一个尺寸元素
如果结束
'下一行或仅第一行将被处理,这一点非常重要
productRow=productRow+1
温德
端接头

您需要循环各种可能性,在单元格中只有一个值的情况下保持值的静态,并在管道字符上使用
Split
将多个值分离到一个数组中,您可以使用该数组将x=LBound(var)到Ubound(var)使用
on.Kevin,非常感谢您快速而出色的回答。我在您的作业“在变体表中设置适当的单元格”中遇到了一些问题。它只显示子SKU(非常有用),所以我猜我的设置有问题。老实说,我从来没有真正使用过VBA,所以我希望你能给我一个更好的主意,在哪里插入单元格以及使用什么代码。再次感谢!@Ludo-看看代码,应该很容易找到。对于变体表中你想要的每一列,你都必须添加一行与此类似:
variationSheet.Cells(variationRow,1).Value=productsSheet.Cells(productRow,1)
其中第一个“1”是变量表中的列号,“=”后面的所有内容这是你在专栏中想要的。如果你想不出来,请告诉我。在你给我指出正确的方向后,这确实很容易。我想我们就快到了。它正在生成所需的专栏。只有大小和颜色变量没有分开。所以每个产品变体都显示L | M | S | XS作为大小,芥末红作为颜色。这是一个I w继续演示我的意思。很高兴你取得了进展!修复非常简单-在变体表中设置大小或颜色时,将其设置为当前元素,而不是产品表中的元素。例如
variationSheet.Cells(variationRow,4)。Value=sizeElement
如果你现在对结果满意,请接受答案:)这就成功了!!!我太激动了!你用这段代码为我节省了几个小时的时间。非常感谢你!