首页 > Excel专区 > Excel函数 >

excel自定义函数获取多个单元格中相同的字符

Excel函数 2023-01-13

要在Excel中获取区域内各单元格字符串中的相同字符,可用下面的自定义函数。例如要在D1单元格获取A1:C1区域各单元格中相同的字符,如图:

按Alt+F11,打开VBA编辑器,单击菜单“插入→模块”,在代码窗口中输入自定义函数:

Function GetDupChars(rRng As Range) As String
Application.Volatile
Dim i As Integer, j As Integer, k As Integer
Dim Str As String
Str = rRng.Item(1).Text
For i = 1 To Len(Str)
k = 0
For j = 2 To rRng.Cells.Count
If InStr(rRng.Item(j).Text, Mid(Str, i, 1)) Then k = k + 1
Next
If k = j – 2 And InStr(GetDupChars, Mid(Str, i, 1)) = 0 _
Then GetDupChars = GetDupChars & Mid(Str, i, 1)
Next
End Function

然后关闭VBA编辑器,在D1单元格中输入公式即可:

=GetDupChars(A1:C1)


Copyright © 2016-2023 office学习教程网 office.tqzw.net.cn. All Rights Reserved.