首页 > 解决方案 > 根据 A 列数据将不同数量的 B 列单元格中的数据转换为单个逗号分隔的行

问题描述

我需要重新格式化从 Shopify 导出的大量图像。

它们已以这种格式导出:

 A        B
SKU | img source
1755  http://img1.jpg
      http://img2.jpg
      http://img3.jpg
1756  http://img1.jpg 
      http://img2.jpg
1757  http://img1.jpg

每个 sku 都有不同数量的图像。我需要将所有 url 放在一行中的逗号分隔列表中(与 sku 相同),然后每当 A 列中出现新值时,它需要以匹配的 B 列值作为列表中的第一个重新启动等等等等。

标签: excelvbaexcel-formula

解决方案


作为记录,这是有效的宏

Sub Delimit()
rownum = 3 'update to the correct starting row
outputRow = 1 'starting row for the output table
lastrow = Cells(Rows.Count, "B").End(xlUp).Row
While rownum <= lastrow
    SKU = Cells(rownum, "A")
    delimited = Cells(rownum, "B")
    rownum = rownum + 1
    While (Cells(rownum, "A") = SKU Or Len(Cells(rownum, "A")) = 0) And rownum <= lastrow
        delimited = delimited & "," & Cells(rownum, "B")
        rownum = rownum + 1
    Wend
    Cells(outputRow, "D") = SKU           'change "D" to appropriate output column
    Cells(outputRow, "E") = delimited     'change "E" to appropriate output column
    outputRow = outputRow + 1
Wend
End Sub

推荐阅读