如果要去掉某個(gè)字符串中的所有數(shù)字,例如將一個(gè)字符串“Excel123”去掉數(shù)字后變?yōu)?ldquo;Excel”,可以用自定義函數(shù)來(lái)實(shí)現(xiàn)。按Alt+F11打開(kāi)VBA編輯器,單擊菜單“插入→模塊”,在右側(cè)的代碼窗口中輸入自定義函數(shù):
Function RemoveNums(theString As String)
Dim eachChar, Temp As String
Dim i As Integer
Temp = ""
For i = 1 To Len(theString)
eachChar = Mid(theString, i, 1)
If Asc(eachChar) < 48 Or Asc(eachChar) > 57 Then
Temp = Temp & eachChar
End If
Next
RemoveNums = Temp
End Function
假如包含數(shù)字的字符串在A1單元格,在B1單元格中輸入
=RemoveNums(A1)
即可返回一個(gè)去掉數(shù)字后的字符串,如果字符串全部由數(shù)字組成,則返回空字符串。