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

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

  发布时间:2023-08-30 01:45:02 

标签:excel,利用,自定义,函数,获取,多个,单元格,中

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

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

    按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