我有一个 Excel 文件,其中一列列出了产品 SKU,另一列列出了产品图片 URL。有超过 5000 行。我的问题是,该文件有多个行用于同一产品,但图片的 URL 不同,因为它们是同一 SKU 的不同颜色。我如何将它们组合起来,以便每个 SKU 包含所有通过“|”分隔的 URL
我尝试在宏中使用下面的代码,但它不会将原始文件中仅有一个 url 的 SKU 复制到第二张表中。哪个宏适合这个?
Sub ReOrganizer()
Dim s1 As Worksheet, s2 As Worksheet
Dim N As Long, i As Long, K As Long
Dim v1 As String, v2 As String
Set s1 = Sheets("Sheet1")
Set s2 = Sheets("Sheet2")
K = 1
N = Cells(Rows.Count, "A").End(xlUp).Row
v1 = s1.Cells(2, 1).Value
v2 = s1.Cells(2, 2).Value
For i = 3 To N
vn1 = s1.Cells(i, 1).Value
vn2 = s1.Cells(i, 2).Value
If vn1 = v1 Then
v2 = v2 & "," & vn2
Else
s2.Cells(K, 1) = v1
s2.Cells(K, 2) = v2
v1 = vn1
v2 = vn2
K = K + 1
End If
Next i
s2.Cells(K, 1) = v1
s2.Cells(K, 2) = v2
子目录结束
答案1
答案2
尝试编写以下算法:
Urlbuffer = ""
Sku = ""
k = 1
N = *max rows*
s1 = *sheet1*
s2 = *sheet2*
For i = 1 to N
Sku = s1.cells(i, 1)
If Sku = s1.cells(i+1) then
Urlbuffer =Urlbuffer & " | " & s1.cells(i,2)
Else
s2.cells(k, 1) = Sku
s2.cells(k, 2) = s1.cells(i, 2) & Urlbuffer
k = k + 1
Urlbuffer = ""
End if
Next i