我有一组行&栏目如下
Column1 Column2 123 Value1 456 Value1 789 Value2 101 Value2 234 Value2 567 Value3 890 Value4 I would like to concatenate column1 based on column2 range like: Column3 123 123,456 789 789,101 789,101,234 567 890
我尝试使用Formula并做到了,但是有更好的方法(比如Macro)吗?
= IF(B2 = B1,C1和安培; “” &安培; C2,C2)
并选择每个值的最后一行
答案 0 :(得分:0)
好吧,这个宏就可以了。不过,我不一定会说它更好!
Sub Macro1()
Dim Source As Range
Dim Control As Range
Dim Output As Range
Set Source = Range("A1")
Set Control = Range("B1")
Set Output = Range("C1")
Dim StoreHere As String
Dim Row As Integer
Dim AddItOn As Boolean
Row = 1
StoreHere = ""
While (Not (IsEmpty(Source.Cells(Row, 1))))
If (Row > 1) Then
If (Control.Cells(Row, 1) = Control.Cells(Row - 1, 1)) Then
AddItOn = True
Else
AddItOn = False
End If
Else
AddItOn = False
End If
If (AddItOn = True) Then
StoreHere = StoreHere & "," & Source.Cells(Row, 1)
Else
StoreHere = Source.Cells(Row, 1)
End If
Output.Cells(Row, 1).NumberFormat = "@"
Output.Cells(Row, 1) = StoreHere
Row = Row + 1
Wend
End Sub
答案 1 :(得分:0)
这是一个较小的选项
Sub Macro1()
Dim cl As Range
Set cl = [A1] ' set to where your data starts
Do While cl <> ""
If cl.Cells(1, 2) = cl.Cells(0, 2) Then
cl.Cells(1, 3) = cl.Cells(0, 3) & "," & cl
Else
cl.Cells(1, 3) = CStr(cl)
End If
Set cl = cl.Cells(2, 1)
Loop
End Sub
答案 2 :(得分:0)