You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Excel中用VBA/UDF自定义函数实现多单元格字符串灵活拼接

自定义VBA UDF实现多单元格字符串快速拼接

这里提供一个自定义VBA函数,直接选中目标单元格区域就能完成字符串拼接,不用反复写CONCATENATE函数:

Function JoinCells(rng As Range, Optional delimiter As String = "") As String
    Dim cell As Range
    Dim resultStr As String
    
    For Each cell In rng
        ' 跳过空单元格,避免拼接多余分隔符
        If cell.Value <> "" Then
            If resultStr <> "" Then
                resultStr = resultStr & delimiter
            End If
            resultStr = resultStr & cell.Value
        End If
    Next cell
    
    JoinCells = resultStr
End Function

使用方法:

  • 打开Excel,按下Alt + F11打开VBA编辑器
  • 右键点击当前工作簿,选择「插入」→「模块」
  • 将上面的代码粘贴到模块窗口中,保存工作簿为「启用宏的工作簿(.xlsm)」
  • 返回工作表,在目标单元格输入公式:
    • 无分隔符拼接:=JoinCells(选中的单元格区域) 比如 =JoinCells(A2:C2)
    • 带分隔符拼接:=JoinCells(选中的单元格区域, "分隔符") 比如 =JoinCells(A2:C2, ", ")

这个函数会自动跳过空单元格,避免出现多余的分隔符,完全匹配你需要的拼接效果。

内容的提问来源于stack exchange,提问作者Aija

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 10:05:56