按条件合并多个excel单元格内容到同一单元格的自定义函数
时间:2023-09-16 17:40:55
Excel中的CONCATENATE函数可以将多个文本字符串联接成一个文本字符串,但有时需要按指定条件合并某些单元格的内容到一个单元格,这时CONCATENATE函数无法实现,而使用下面的自定义函数就十分方便了。如图所示,需要在F2单元格中将“调资额”为“190”的“姓名”用“、”连接起来。
步骤如下:
1.按Alt+F11,打开VBA编辑器,单击菜单“插入→模块”,在代码窗口中输入下列代码:
Function CONCATENATEIF(rng1 As Range, rng2 As Range, criteria As String, separator As String) As String
Dim arr()
Dim rCell As Range
Dim i As Integer, j As Integer
On Error Resume Next
j = WorksheetFunction.CountIf(rng2, criteria)
If j > 0 Then
ReDim arr(0 To j – 1)
For Each rCell In rng2
If WorksheetFunction.CountIf(rCell, criteria) Then
arr(i) = rng1.Item(1).Offset(rCell.Row – rng2.Row, rCell.Column – rng2.Column).Value
i = i + 1
End If
Next
For i = 0 To j – 1
CONCATENATEIF = CONCATENATEIF & arr(i) & IIf(i <> j – 1, separator, "")
Next
End If
End Function
上述代码定义了一个自定义函数CONCATENATEIF,共有四个参数,分别为需要连接单元格内容的区域(通常为单行或单列)、条件区域,条件和分隔符。使用该自定义函数时注意:
①参数中的两个区域的形状大小需相同;
②条件的输入类似COUNTIF函数,如 190、">100"、"苹果" 等,可使用通配符“*”、“?”。
2.返回Excel工作表界面,在F2单元格中输入公式:
=CONCATENATEIF($B$2:$B$16,$C$2:$C$16,E2,"、")
即可得到所需的结果。
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Excel怎么移动数据到有内容的区域
![](https://img.aspxhome.com/file/2023/2/a153782_0s.jpg)
Win8系统电脑开始菜单鼠标右键没有反应的解决方法
![](https://img.aspxhome.com/file/2023/2/a286885_0s.jpg)
Win10电脑预览体验计划错误提示:0x0怎么解决?
![](https://img.aspxhome.com/file/2023/9/51849_0s.png)
WPS文字2013中边框线型的快捷输入有什么方法
ROG 枪神6 Plus 超竞版笔记本安装win11系统教程
![](https://img.aspxhome.com/file/2023/30/a266414_0s.png)
excel表格数据怎么显示百分比?excel表格数据显示百分比教程
![](https://img.aspxhome.com/file/2023/30/a268437_0s.jpg)
一首歌里的十个Excel问题
影子系统迁移目录是什么意思_影子系统迁移目录目的
![](https://img.aspxhome.com/file/2023/3/a298779_0s.jpg)
Win10文件资源管理器左边不见了怎么解决?文件夹左边目录如何显示
![](https://img.aspxhome.com/file/2023/1/a275473_0s.png)
word表格加一列的方法
![](https://img.aspxhome.com/file/2023/9/34229_0s.png)
雨林木风win10 u盘安装教程?U盘安装雨林木风win10系统
![](https://img.aspxhome.com/file/2023/5/47645_0s.jpg)
如何在wps设置自动生成目录
Win8系统中的xlueops.exe进程删除方法
![](https://img.aspxhome.com/file/2023/4/a308806_0s.jpg)
win7系统浏览器被t999劫持找不到mssafel.sys怎么办?
![](https://img.aspxhome.com/file/2023/4/a311479_0s.png)
钉钉网页版登录入口_钉钉网页版登录首页分享
![](https://img.aspxhome.com/file/2023/7/a327663_0s.png)
小爱音箱怎么连接电脑外放 小爱音箱怎么连接电脑当音响
![](https://img.aspxhome.com/file/2023/1/a272869_0s.jpg)
苹果笔记本双系统装win10还是win7呢?
![](https://img.aspxhome.com/file/2023/1/a277363_0s.png)
Ubuntu使用apt-get更新时选择IPv4或IPv6通道的方法
![](https://img.aspxhome.com/file/2023/3/a292376_0s.jpg)
(图)Win10 Build 10135上手体验图集:RTM预览版近了
![](https://img.aspxhome.com/file/2023/28/a249835_0s.jpg)
高手支招教你Win8删除右键菜单选项的解决技巧
![](https://img.aspxhome.com/file/2023/3/a300212_0s.jpg)