如何将 2 列中的数据与用符号分隔的值组合起来?

How to combine data from 2 columns with values separated by a symbol?

第 1 列中的数据包含以“#”符号分隔的数据类型:

Year#Storey#Area#Condition#Name

第 2 列中的数据包含与第 1 列中的数据类型相对应的信息,也以“#”符号分隔:

2015#3#170#Renovated#John

我想合并第 1 列和第 2 列的信息并获取以下格式的数据:

Year - 2015
Storey - 3
Area - 170
Condition - Renovated
Name - John

为了澄清,我提供了我想要得到的图片:

如何在 Excel 中实施?我需要指定什么样的公式?

您可以通过在 VBA 中创建 UDF 来避免从数据行中提取列数据的所有麻烦。

为此,请在您的工作簿中创建一个 VBA 模块并放置以下代码:

Function GETCOLUMNDATA(Cell As Range, ColumnNumber As Long, Delimeter As String) As String
    GETCOLUMNDATA = Split(Cell, Delimeter)(ColumnNumber - 1)
End Function

然后就可以轻松提取数据了,如图:

公式显示在公式栏中。

然后对于您想要的格式,只需相应地加入它们即可。

注意:要在单元格中换行,请按 Alt+Enter

您可以使用相对简单的用户定义函数来完成此操作。

Option Explicit
Function CombineCells(R1 As Range, R2 As Range, Optional Sep As String = "#")
    Dim V1 As Variant, V2 As Variant
    Dim I As Long

V1 = Split(R1, Sep)
V2 = Split(R2, Sep)

'Check that same number of items in each
If UBound(V1) <> UBound(V2) Then
    MsgBox Prompt:="Data Error" & vbLf & "Item Count different in the Two Cells", Title:="Input Error"
End If

For I = 0 To UBound(V1)
    V1(I) = V1(I) & " - " & V2(I)
Next I

CombineCells = Join(V1, vbLf)

End Function

要输入此用户定义函数 (UDF),alt-F11 打开 Visual Basic 编辑器。 确保您的项目在 Project Explorer window 中突出显示。 然后,从顶部菜单 select Insert/Module 和 将下面的代码粘贴到打开的 window 中。

要使用此用户定义函数 (UDF),请输入类似

的公式
=CombineCells(A2,B2)

在某个单元格中。