将多个单元格合并成一个具有宏的excel? [英] Combine multiple cells into one in excel with macro?

查看:301
本文介绍了将多个单元格合并成一个具有宏的excel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似的问题:

使用Excel中的VBA将2个单元格的内容合并到另一个第三个单元格中

但是我想组合一列中的单元格范围,例如A2:A50。有时,我有超过300个细胞被组合成一个细胞。价值观是文字。有没有办法修改这个宏,以便它在一个范围而不是两个单元格上工作?

But I want to combine a range of cells within a column, eg A2:A50. Sometimes I have over 300 cells to be combined into one. Values are text. Is there any way to modify this macro so that it works on a range instead of just two cells?

谢谢!

推荐答案

根据你引用的线程,我想你希望返回单元格所持有的所有值的并置,将所有值解释为字符串?

Based on the thread you are citing, I guess you wish to return the concatination of all the values held by the cells, interpreting all the values as strings?

为此,您可以使用如下所示的VBA宏:

For that, you could use a VBA macro that looks like this:

Function ConcatinateAllCellValuesInRange(sourceRange As Excel.Range) As String
    Dim finalValue As String

    Dim cell As Excel.Range

    For Each cell In sourceRange.Cells
        finalValue = finalValue + CStr(cell.Value)
    Next cell

    ConcatinateAllCellValuesInRange = finalValue
End Function

举个例子,你可以像这样调用:

As an example, you could call it like this:

Sub MyMacro()
    MsgBox ConcatinateAllCellValuesInRange([A1:C3])
End Sub

这是你要找的吗?

Mike

这篇关于将多个单元格合并成一个具有宏的excel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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