要在Excel中獲取區(qū)域內(nèi)各單元格字符串中的相同字符,可用下面的自定義函數(shù)。例如要在D1單元格獲取A1:C1區(qū)域各單元格中相同的字符,如圖:
按Alt+F11,打開VBA編輯器,單擊菜單“插入→模塊”,在代碼窗口中輸入自定義函數(shù):
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)