在标准模块中输入以下UDF:在A1
Public Function xlate(s As String) As String
Dim N As Long, i As Long, CH As String
N = Len(s)
xlate = ""
If N < 3 Then
xlate = s
Exit Function
End If
For i = 2 To N - 1
CH = Mid(s, i, 1)
If CH <> "O" Then
xlate = xlate & CH
Else
If IsNumeric(Mid(s, i - 1, 1)) And IsNumeric(Mid(s, i + 1, 1)) Then
xlate = xlate & "o"
Else
xlate = xlate & "O"
End If
End If
Next i
xlate = Left(s, 1) & xlate & Right(s, 1)
End Function
与数据
然后,在B1输入:
=xlate(A1)
和复制下来
个
用户定义函数(UDF)是非常容易安装和使用:
- ALT-F11带来了VBE窗口
- ALT-I ALT + M打开一个新的模块
- 粘贴的东西并关闭VBE窗口
如果保存工作簿,则UDF将与其一起保存。 如果您正在使用一个版本的Excel更高然后2003,你必须保存 该文件作为.XLSM而非的.xlsx
要删除UDF:
- 弹出窗口VBE如上
- 清晰的代码进行
- 关闭VBE窗口
从Excel使用UDF:
= XLATE(A1)
要了解更多关于一般的宏,请参阅:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
和
http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx
以及有关UDF的细节,请参见:
http://www.cpearson.com/excel/WritingFunctionsInVBA.aspx
必须启用宏才能使其工作!
EDIT#1
这里是更新的代码来处理终端ø
Public Function xlate(s As String) As String
Dim N As Long, i As Long, CH As String
N = Len(s)
xlate = ""
If N < 3 Then
xlate = s
Exit Function
End If
For i = 2 To N - 1
CH = Mid(s, i, 1)
If CH <> "O" Then
xlate = xlate & CH
Else
If IsNumeric(Mid(s, i - 1, 1)) And IsNumeric(Mid(s, i + 1, 1)) Then
xlate = xlate & "o"
Else
xlate = xlate & "O"
End If
End If
Next i
xlate = Left(s, 1) & xlate
If Right(s, 1) = "O" And IsNumeric(Right(xlate, 1)) Then
xlate = xlate & "o"
Else
xlate = xlate & Right(s, 1)
End If
End Function
作为首字母或尾字符的** O **如何? – 2014-10-10 13:27:55
它不会工作,因为我需要它只能被替换,如果有一个整数在它之前,而不是一个字符 – user2062207 2014-10-10 13:39:32