有这样一个表格,一列为类型,一列为数据,如何以类型合并数据在一行中呢?如下图。组别和姓名。按组别合并姓名在同一行。
这里我们自定义一个函数,可以通过该函数自动组合合并单元格到同一行中
Function hb(Rng1 As Range, Str, Rng2 As Range) Dim Arr, Brr Dim i As Long Dim j As Long Dim MyStr As String If Rng1.Rows.count > 65536 Then Arr = Rng1.Resize(65536, Rng1.Columns) Brr = Rng2.Resize(65536, Rng1.Columns) Else Arr = Rng1 Brr = Rng2 End If For i = 1 To UBound(Arr) For j = 1 To UBound(Arr, 2) If Arr(i, j) <> "" Then If Arr(i, j) = Str Then MyStr = MyStr & Brr(i, j) & "," End If Else Exit For End If Next j Next i hb = Left(MyStr, Len(MyStr) - 1) End Function
解析:
第三参数:(必须)要连接的实际单元格区域。
先填写分组的内容,在E2写入公式:=hb(A$1:A$7,D2,B$1:B$7)
参考自:Office自学教程网