电脑教程
位置:首页>> 电脑教程>> office教程>> excel自定义函数获取多个单元格中相同的字符

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

  发布时间:2022-03-27 16:42:58 

标签:函数,单元格,自定义,要在,Excel函数

要在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)

0
投稿

猜你喜欢

手机版 电脑教程 asp之家 www.aspxhome.com