Excel 如何在创建范围的行中循环,并在该范围内循环,并在值更改时重新开始

Excel 如何在创建范围的行中循环,并在该范围内循环,并在值更改时重新开始,excel,vba,Excel,Vba,嗨,我正试图在Excel中的一些行中循环以收集信息并将其粘贴到其他工作表中。基本上,我需要循环行,并根据产品名称指定范围,以便下一行有新产品名称时,范围重新开始。这将收集同一产品的所有属性,并且能够仅在新范围/新工作表的第一行中插入由coma分隔的属性,然后将从给定文件复制变体 我可以将原始工作表中的所有变体复制到新工作表中,但是第一行的所有属性(大小/颜色)都应该在一个单元格中。我设法遵循我在这里找到的一些代码,但我只获取属性数组并创建变体。应根据此范围的信息创建父产品 Sub Product

嗨,我正试图在Excel中的一些行中循环以收集信息并将其粘贴到其他工作表中。基本上,我需要循环行,并根据产品名称指定范围,以便下一行有新产品名称时,范围重新开始。这将收集同一产品的所有属性,并且能够仅在新范围/新工作表的第一行中插入由coma分隔的属性,然后将从给定文件复制变体

我可以将原始工作表中的所有变体复制到新工作表中,但是第一行的所有属性(大小/颜色)都应该在一个单元格中。我设法遵循我在这里找到的一些代码,但我只获取属性数组并创建变体。应根据此范围的信息创建父产品

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
温德
端接头
谢谢你的帮助,谢谢