Excel – 在数字和单词之间添加空格

我正在寻找一种方法来在文本值和数值之间放置一个空格,但是有一个困难。 有时文本也可能以数字开始,我不希望这包含空格。 例如

Col A Col B Name1:3-2 Name 1:3-2 Name6:5,4 Name 6:5,4 1 Val55:12-4 1 Val 55:12-4 2 Val 22:43 2 Val 22:43 Name10 Name 10 

其中Col A是值,Col B包含将值增加空间的公式是Col A.

几件事要注意这里:

  1. 文本结束后,空格总是只添加到第一个数字。

  2. 如果值以一个数字开始,那么应该忽略一个数字,并且文本之后的第一个数字应该添加空格

  3. 如果已有空间,则不应添加另一个空间。

  4. 数字之前的文本长度各不相同

  5. 在第一个数字之后的值之间并不总是:

我正在使用的数据集大约有1,000个条目,所以速度并不是必要的,只需要一些适用于所有情况的东西,因为我不想经过很多条目并添加空间。

编辑最终解决scheme感谢以下加里的学生:

 ' Function Assumes that there are atleast 2 characters in front of the expected space ' Function Assumes that there are no numbers within the text that will cause an early splitting of characters Public Function SpacedOut(sIn As String) As String Dim L As Long, i As Long, Done As Boolean Dim sOut As String L = Len(sIn) Done = False sOut = Left(sIn, 1) ' Skips the first possible number if it is there if not, we are safe to start at 2 ' Since there will always be more than one character prior to the expected first number For i = 2 To L ' Check for a number without a space before it If Mid(sIn, i - 1, 1) <> " " And Mid(sIn, i, 1) Like "[0-9]" And Not Done Then Done = True sOut = sOut & " " & Mid(sIn, i, 1) ' Check for a space with a number after it and continue on if this is found ElseIf Mid(sIn, i - 1, 1) = " " And Mid(sIn, i, 1) Like "[0-9]" And Not Done Then Done = True sOut = sOut & Mid(sIn, i, 1) ' Append next character Else sOut = sOut & Mid(sIn, i, 1) End If Next i SpacedOut = sOut End Function 

谢谢,DMAN

试试这个小UDF

 Public Function SpacedOut(sIn As String) As String Dim L As Long, i As Long, Done As Boolean Dim sOut As String L = Len(sIn) Done = False sOut = Left(sIn, 1) For i = 2 To L If Mid(sIn, i - 1, 1) Like "[a-zA-Z]" And Mid(sIn, i, 1) Like "[0-9]" And Not Done Then Done = True sOut = sOut & " " & Mid(sIn, i, 1) Else sOut = sOut & Mid(sIn, i, 1) End If Next i SpacedOut = sOut End Function 

编辑#1:

这里是我的VBE屏幕与模块高亮显示的快照:

SNAP