Excel VBA
Excel VBA提取单元格中字符串的重复字符
2017-09-29 16:41:17

在Excel表格中,有时需要从单元格中提取重复的字符。如从“Office中国-Office教程”中,提取出现次数为2以上的“Office”。效果图如下

详细源码:

Function GetDupChar(theString As String) As String     Dim i As Integer     For i = 1 To Len(theString) - 1         If Len(theString) - Len(Replace(theString, Mid(theString, i, 1), "")) > 1 Then '             If InStr(GetDupChar, Mid(theString, i, 1)) = 0 Then GetDupChar = GetDupChar & Mid(theString, i, 1)         End If     Next End Function

例如单元格的字符串在A列,在D列单元格中输入公式:=GetDupChar(A1)  即可提取相同的字符

如要将多个单元格中的字符串合并后提取重复字符,如A1单元格为“Office中国-Office教程”,A2单元格为“Excel-cn Excel教程”,在E1单元格输入公式:=GetDupChar(A1&A2)

将返回这两个单元格中字符串合并后重复的字符“Office-教程x”。