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

第一列中的数据包含用'#'符号分隔的数据types:

Year#Storey#Area#Condition#Name 

第二列中的数据包含第一列中的数据types所对应的信息,也用“#”符号分隔:

 2015#3#170#Renovated#John 

我想结合第一列和第二列的信息,并获取以下格式的数据:

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

为了澄清我提供了我想要得到的图片: height =“50”width =“50”

何在Excel中实现它? 我需要指定什么样的配方?

你可以用一个相对简单的用户定义函数来做到这一点。

 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 

要input这个用户自定义函数(UDF), alt-F11打开Visual Basic编辑器。 确保您的项目在“项目浏览器”窗口中突出显示。 然后,从顶部菜单中selectInsert/Module然后将下面的代码粘贴到打开的窗口中。

要使用这个用户定义函数(UDF),input一个像

 =CombineCells(A2,B2) 

在一些细胞。

您可以通过在VBA中创buildUDF来避免所有头痛的问题从数据行中提取列数据。

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

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

然后您可以轻松提取数据,如下所示:

公式显示在公式栏中。

在这里输入图像描述

然后根据你想要的格式,相应地join他们。

在这里输入图像说明

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