Excel宏 - 逗号分隔单元格行(保留/聚合列) [英] Excel Macro - Rows to Comma Separated Cells (Preserve/Aggregate Column)

查看:218
本文介绍了Excel宏 - 逗号分隔单元格行(保留/聚合列)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据此数据:

 <  -  A(类别) - > <  -  B(项目) - > 
1 Cat1 a
2 Cat1 b
3 Cat1 c
4 Cat2 d
5 Cat3 e
6 Cat4 f
7 Cat4 g

我需要这样:

 <  -  A(类别) - > <  -  B(项目) - > 
1 Cat1 a,b,c
2 Cat2 d
3 Cat3 e
4 Cat4 f,g


解决方案

如果您想保留原始数据,并仅在其他地方汇总数据,可以使用以下方法。

在VB中创建一个基本上就像CONCATENATE一样的用户定义函数,但可以在数组公式中使用。这将允许您在CONCATENATE函数中为范围变量添加IF语句。这是一个快速版本扔在一起:

 私有功能CCARRAY(rr As Variant,sep As String)
'rr是要连接的值的范围或数组。 sep是分隔符。
Dim rra()As Variant
Dim out As String
Dim i As Integer

关于错误GoTo EH
rra = rr

out =
i = 1

尽管i = UBound(rra,1)
如果rra(i,1)< False然后
out = out& rra(i,1)& sep
End If
i = i + 1
Loop
out = Left(out,Len(out) - Len(sep))

CCARRAY = out
退出函数

EH:
rra = rr.Value
简历Next

结束函数

所以你可以使用下表来总结项目:

  {= CCARRAY(IF(A1:A7 =Cat1,B1:B7),,)} 

记住在输入公式时按Ctrl + Shift + Enter。


Based on this Data:

    <- A (Category) ->   <- B (Items) -> 
1   Cat1                 a
2   Cat1                 b
3   Cat1                 c
4   Cat2                 d
5   Cat3                 e
6   Cat4                 f
7   Cat4                 g

I need this:

    <- A (Category) ->   <- B (Items) -> 
1   Cat1                 a,b, c
2   Cat2                 d
3   Cat3                 e
4   Cat4                 f, g

解决方案

If you want to keep your original data and merely summarize the data somewhere else, you can use the following method.

Create a user-defined function in VB that is essentially just like CONCATENATE, but can be used in an array formula. This will allow you to stick an IF statement in for the range variable in the CONCATENATE function. Here's a quick version I threw together:

Private Function CCARRAY(rr As Variant, sep As String)
'rr is the range or array of values you want to concatenate.  sep is the delimiter.
Dim rra() As Variant
Dim out As String
Dim i As Integer

On Error GoTo EH
rra = rr

out = ""
i = 1

Do While i <= UBound(rra, 1)
    If rra(i, 1) <> False Then
        out = out & rra(i, 1) & sep
    End If
    i = i + 1
Loop
out = Left(out, Len(out) - Len(sep))

CCARRAY = out
Exit Function

EH:
rra = rr.Value
Resume Next

End Function

So you could use the following in this table to summarize Items:

{=CCARRAY(IF(A1:A7="Cat1",B1:B7),", ")}

Remember to press Ctrl+Shift+Enter when entering the formula.

这篇关于Excel宏 - 逗号分隔单元格行(保留/聚合列)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆