Как можно циклически проходить по строкам, создавая диапазоны, и циклировать внутри этих диапазонов и начинать заново при изменении значения - PullRequest
2 голосов
/ 11 июня 2019

Привет. Я пытаюсь перебрать некоторые строки в 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

Любая помощь приветствуется. Спасибо

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...