Excel VBA:合并/合并具有相同起始字符的string

我不能在任何解决scheme上工作,也没有在网上find答案,也许你可以帮助我

我想在VBA中合并2个单元格,如下所示:
cell1: foo_123
cell2: foo_456
outputCell: foo_123_456

因此,代码应该看到cell1和cell2具有foo_的共同点,并删除foo_部分

输出看起来像这样:
outputCell = cell1 & "_" & cell2
(outputCell = foo_123&“_”&456)

这里有一个简单的函数应该可以做到这一点(我使用了string,而不是单元格,根据您的数据,您可能希望添加一些angular落案例,但总体思路应该清晰):

 Function merge(value1 As String, value2 As String) As String Dim result As String result = value1 For counter = 1 To Len(value2) If Len(value1) < counter Then ' Edit SLT: changed <= to <' result = result & "_" & Mid(value2, counter) Exit For: Else If Not Mid(value1, counter, 1) = Mid(value2, counter, 1) Then result = result & "_" & Mid(value2, counter) Exit For: End If End If Next merge = result End Function