代码来结合文本string的公共部分

如果我有一个电子表格中的string列表,有没有什么办法可以将它们结合起来,只保留它们的共同string? 例如,如果我有这个列表:

C- D2 Carbon steel column 1 7.58 0.47 1.15 1,096.00 C-E1 Carbon steel column 1 7.58 0.47 1.15 1,096.00 C- E2 Carbon steel column 1 7.58 0.47 1.15 1,096.00 C-F1 Carbon steel column 1 7.58 0.47 1.15 1,096.00 C-F2 Carbon steel column 1 7.58 0.47 1.15 1,096.00 C-G1 Carbon steel column 1 7.58 0.47 1.15 1,096.00 C-G2 Carbon steel column 1 7.58 0.47 1.15 1,096.00 C-H1 Carbon steel column 1 7.58 0.47 1.15 1,096.00 

…我想把它结合起来

 Carbon steel column 8 7.58 0.47 1.15 1,096.00 

做数字不是问题,但是我如何获取文本string的常用元素?

编辑:澄清,目的是find共同的元素,不只是把它们分开。 不幸的是,事先并不知道结束语。

在B11:B12中,作为这些SUMIF函数之一 ,

 =SUMIF($A$2:$A$9, "*Carbon steel column", B$2:B$9) '◄ B11 =SUMIF($A$2:$A$9, "*"&$A12, B$2:B$9) '◄ B12 

请注意预configuration通配符星号。 这意味着A列将以碳钢柱结束。 填写正确的额外列总数。 用AVERAGEIF函数或其他聚合函数代替以达到所需的结果。

carbno_steel_column

您可以尝试拆分组件中的第一个单元格(假设是空格),并将所有单词添加到集合中。 然后,对于每个其他单元格,将其拆分并检查集合是否已经包含单词。 如果没有,从集合中删除它。

 Dim MyCollection As Collection Dim i As Integer Dim j As Integer Dim k As Integer Dim bIsInArray As Boolean Dim sFinalString As String Private Sub CommandButton1_Click() lLastRow = Cells(Rows.Count, 1).End(xlUp).Row 'get the number of rows with data Set MyCollection = New Collection ReDim MyArray(1 To 1) As String For i = 2 To lLastRow 'Assuming your data starts in A2 (A1 being titles) If i = 2 Then 'The first data cell MyArray = Split(Worksheets("Data").Cells(i, 1).Value, " ") 'split the cell using spaces as spliting character For j = 0 To UBound(MyArray) MyCollection.Add (MyArray(j)) 'add all words to collection Next Else 'if is not the first cell of data ReDim MyArray(1 To 1) As String 'set a "new" array MyArray = Split(Worksheets("Data").Cells(i, 1).Value, " ") For j = MyCollection.Count To 1 Step -1 bIsInArray = False For k = 0 To UBound(MyArray) If MyCollection.Item(j) = MyArray(k) Then bIsInArray = True End If Next If bIsInArray = False Then MyCollection.Remove (j) End If Next End If Next 'Now MyCollection contains all common words sFinalString = "" For j = 1 To MyCollection.Count sFinalString = sFinalString & " " & MyCollection.Item(j) Next Worksheets("Data").Cells(lLastRow + 2, 1).Value = sFinalString End Sub