Excel很强大,但有些时候我们需要完成一些自定义的功能,则需要用到宏,然后在Excel中调用该宏进行执行。现在以 大写字母前加空格为例,实现效果如下图
首先打开要编辑的Excel表格,然后按Alt+F11打开微软的VBA编程器,
然后单击VBAProject,右键点击插入模块
在模块中增加如下脚本
Function AddSpaces(str As String) As String
Dim xOut As String
'判断第一个字符是否为小写字母,如果为小写字母则将其改为大写字母
If Asc(Left(str, 1)) >= 95 And Asc(Left(str, 1)) <= 122 Then
xOut = UCase(Left(str, 1))
Else
xOut = Left(str, 1)
End If
'判断是否为大写字母,是则添加空格
For i = 2 To Len(str)
If Asc(Mid(str, i, 1)) >= 65 And Asc(Mid(str, i, 1)) <= 90 Then
xOut = xOut & " "
End If
xOut = xOut & Mid(str, i, 1)
Next
AddSpaces = xOut
End Function
然后点击关闭VBA编程器,在Excel中输入AddSpaces()函数即可以使用了。
这样即可以实现该功能。