【问题标题】:Use Excel/VB to create a list of product variations based on list of parent products使用 Excel/VB 根据父产品列表创建产品变体列表
【发布时间】:2014-11-02 00:24:18
【问题描述】:

我正在使用产品 CSV 导入套装插件经营一家 Woocommerce 商店。我需要首先上传一个包含父产品的列表,然后是一个包含产品变体的列表。创建第二个列表非常耗费人力,因此我正在寻找一种方法来尽可能自动生成此列表。

下面是一个简短的父产品列表的样子:

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

产品变体的结果应如下所示:

如您所见,可以看到产品变体(子)固有父产品的所有产品特性以及尺寸和颜色变量之一。如果子 sku 也能自动生成就好了。

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

有人对如何在 Excel 中执行此操作有任何想法吗?我不是 Excel 向导,所以希望您能给我一些关于如何实施您的解决方案的指导。

【问题讨论】:

  • 您需要遍历可能性,在单元格有单个值的情况下保持值静态,并在管道字符上使用 Split 将多个值分隔成一个可以使用的数组For x = LBound(var) to Ubound(var) 开启。

标签: excel woocommerce vba


【解决方案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

【讨论】:

  • 凯文,非常感谢您快速而出色的回答。我在您的分配中遇到了一些问题:“在变体表中设置适当的单元格”。它只显示儿童 SKUS(计算得很好),所以我想我的设置有问题。老实说,我从来没有真正使用过 VBA,所以我希望你能给我一个更好的想法,在哪里插入单元格以及使用什么代码。再次感谢!
  • @Ludo - 看看代码应该很容易理解。对于您想要在变体表中的每一列,您必须添加与此类似的行:variationSheet.Cells(variationRow, 1).Value = productsSheet.Cells(productRow, 1) 其中第一个“1”是变体表中的列号,“=”之后的所有内容都是您想要的列。如果你不明白,请告诉我。
  • 在你指出我正确的方向之后,这确实很容易。我想我们快到了。它正在生成所需的列。只有大小和颜色变量没有分开。因此,每个产品变体均显示 L|M|S|XS 为尺寸,芥末色|红色为颜色。这是sample file,我努力表达我的意思。
  • 很高兴你取得了进展!修复非常简单 - 在变体表中设置大小或颜色时,将其设置为当前元素,而不是产品表中的元素。例如variationSheet.Cells(variationRow, 4).Value = sizeElement如果您对现在的结果感到满意,请接受作为答案:)
  • 成功了!!!我非常兴奋!你刚刚用这段代码为我节省了几个小时的时间。非常感谢!
猜你喜欢
  • 2023-03-05
  • 2021-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-16
相关资源
最近更新 更多