excel自定义函数获取多个单元格中相同的字符
- 2023-02-25
- 来源/作者: Wps Office教程网/ 菜鸟图库
- 156 次浏览
要在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)