先來看看一個表,也不知道哪個挨千刀得,在記錄數(shù)據(jù)得時候搞成這個樣子,都聚集在A1單元格。
要將A1中得姓名和銀行卡號分別提取出來,只需要一個自定義函數(shù)GetChar就可以輕松搞定。
C2單元格:
=INDEX(GetChar($A$1,3),ROW(1:1))
D2單元格:
=INDEX(GetChar($A$1,1),ROW(1:1))
這個神秘得GetChar到底是什么呢?它是VBA自定義函數(shù)。
右鍵單擊工作表標(biāo)簽,插入,模塊,然后將以下代碼粘貼到模塊里,好了,接下來就一起輕松玩轉(zhuǎn)提取字符吧:
Function GetChar(strChar As String, varType As Variant) '取值函數(shù) Dim objRegExp As Object Dim objMatch As Object Dim strPattern As String Dim arr Set objRegExp = CreateObject("vbscript.regexp") varType = LCase(varType) Select Case varType Case 1, "number" strPattern = "-?\d+(\.\d+)?" Case 2, "english" strPattern = "[a-z]+" Case 3, "chinese" strPattern = "[\u4e00-\u9fa5]+" End Select With objRegExp .Global = True .IgnoreCase = True .Pattern = strPattern Set objMatch = .Execute(strChar) End With If objMatch.Count = 0 Then GetChar = "" Exit Function End If ReDim arr(0 To objMatch.Count - 1) For Each cell In objMatch arr(i) = objMatch(i) i = i + 1 Next GetChar = arr Set objRegExp = Nothing Set objMatch = NothingEnd Function
這個自定義函數(shù)得功能設(shè)計就是專門提取字符得,語法為:
GetChar(strChar,varType):
第二參數(shù)為1時提取數(shù)字
第二參數(shù)為2時提取英文
第二參數(shù)為3時提取漢字
圖文感謝作者分享:翟振福