答案 0 :(得分:1)
假设第1行是标题行,所以实际数据从第2行开始,并且您希望输出在单元格J2中开始,此代码应该适合您:
Sub tgr()
Dim cllSKU As Collection
Dim SKUCell As Range
Dim rngFound As Range
Dim arrData(1 To 65000, 1 To 2) As Variant
Dim strFirst As String
Dim strJoin As String
Dim DataIndex As Long
Set cllSKU = New Collection
With Range("G3", Cells(Rows.Count, "G").End(xlUp))
On Error Resume Next
For Each SKUCell In .Cells
cllSKU.Add SKUCell.Text, SKUCell.Text
If cllSKU.Count > DataIndex Then
DataIndex = cllSKU.Count
arrData(DataIndex, 1) = SKUCell.Text
arrData(DataIndex, 2) = Cells(SKUCell.Row, "A").Text & " - ("
Set rngFound = .Find(SKUCell.Text, .Cells(.Cells.Count), xlValues, xlWhole)
If Not rngFound Is Nothing Then
strFirst = rngFound.Address
Do
arrData(DataIndex, 2) = arrData(DataIndex, 2) & Cells(rngFound.Row, "H").Text & ","
Set rngFound = .Find(SKUCell.Text, rngFound, xlValues, xlWhole)
Loop While rngFound.Address <> strFirst
End If
arrData(DataIndex, 2) = Left(arrData(DataIndex, 2), Len(arrData(DataIndex, 2)) - 1) & ")"
End If
Next SKUCell
On Error GoTo 0
End With
If DataIndex > 0 Then
Range("J2:K" & Rows.Count).ClearContents
Range("J2:K2").Resize(DataIndex).Value = arrData
End If
Set cllSKU = Nothing
Set SKUCell = Nothing
Set rngFound = Nothing
Erase arrData
End Sub
答案 1 :(得分:0)
可以在没有.VBA的情况下实现,但无可否认,比复制代码更适合你!: 假设您的标题位于第3行。
=IF(COLUMN()<COUNTIF($G:$G,$G3)+8,IF($G3=$G4,INDIRECT("$h"&ROW()+COLUMN()-8),""),"")
=H3&","&I3&","&J3&","&K3&","&L3 (extended as required)
=A3&" - "&"("&M3
(将M
替换为步骤3中列的列引用)
并将两者复制到适合。 ,,
。=IF(RIGHT(N3,1)=",",LEFT(N3,LEN(N3)-1)&")",N3&")")
=G2=G3
Merged
放入B2。请注意,这不会为您提供问题中显示的M和XL之间的空格。