Excel-在数字和单词之间添加空格
我正在寻找一种方法,在文本值和数字值之间放置一个空格,但有一个问题。有时文本也可能以数字开头,我不希望它包含空格。比如说Excel-在数字和单词之间添加空格,excel,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 Nam
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
其中A列为值,B列包含将空格添加到A列值的公式
这里需要注意的几点:
' 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
”函数假定预期空格前面至少有2个字符
'函数假定文本中没有会导致字符提前拆分的数字
公共函数SpacedOut(sIn作为字符串)作为字符串
Dim L作为Long,i作为Long,doe作为Boolean
暗声如弦
L=Len(sIn)
完成=错误
sOut=左(sIn,1)
跳过第一个可能的数字如果它在那里如果没有,我们可以从2开始
'因为在预期的第一个数字之前总是有多个字符
对于i=2到L
'检查前面没有空格的数字
如果Mid(sIn,i-1,1)”和Mid(sIn,i,1)喜欢“[0-9]”并且没有完成,那么
完成=正确
sOut=sOut&&&Mid(sIn,i,1)
'检查后面有数字的空格,如果找到则继续
ElseIf Mid(sIn,i-1,1)=“和Mid(sIn,i,1)喜欢“[0-9]”然后就不做了
完成=正确
sOut=sOut&Mid(sIn,i,1)
'追加下一个字符
其他的
sOut=sOut&Mid(sIn,i,1)
如果结束
接下来我
SpacedOut=sOut
端函数
谢谢,
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屏幕的快照,其中模块hi亮起:
试试这个小的自定义项:
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屏幕的快照,其中模块hi亮起:
试试这个小的自定义项:
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屏幕的快照,其中模块hi亮起:
试试这个小的自定义项:
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屏幕的快照,其中模块hi亮起:
如果在值“Name”和“Val”之后需要空格,我们可以使用find和replace?因此A列是您所拥有的,B列是您所需要的。是吗?如果在值“Name”和“Val”之后需要空格,我们可以使用find和replace?所以A列是您所拥有的,B列是您所需要的。是吗?如果在值“Name”和“Val”之后需要空格,我们可以使用find和replace?所以A列是您所拥有的,B列是您所需要的。是吗?如果在值“Name”和“Val”之后需要空格,我们可以使用find和replace?所以A列是您所拥有的,B列是您所需要的。是吗?很好用。这就是OP想要的:)你好,谢谢你的回答,我会试试看。你能就如何在谷歌电子表格中使用宏提出建议吗?或者我需要将结果传输到Excel然后返回到google电子表格吗?我也想知道如何在google文档中运行宏!哈哈,好吧,那我该如何在Excel 2010中实现呢?我添加了开发工具、启用脚本并添加了模块。当我使用函数时,我只得到#Name?在字段中,指示它不知道函数的名称。但它确实会出现在可用函数列表中。可能我的角色集不受支持。这是一段实时数据的示例,也许您可以帮助将其分离创世记1:1该错误消息表示Excel找不到自定义项。。。。。。。确保它位于标准模块中。。。。。。。。。。。。确保你像这样使用它:*=SpacedOut(B9)**。工作完美。这就是OP想要的:)你好,谢谢你的回答,我会尝试一下。你能就如何在谷歌电子表格中使用宏提出建议吗?或者我需要将结果传输到Excel然后返回到google电子表格吗?我也想知道如何在google文档中运行宏!哈哈,好吧,那我该如何在Excel 2010中实现呢?我添加了开发工具、启用脚本并添加了模块。当我使用函数时,我只得到#Name?在字段中,指示它不知道函数的名称。但它确实会出现在可用函数列表中。可能我的角色集不受支持。这是一段实时数据的示例,也许您可以帮助将其分离创世记1:1该错误消息表示Excel找不到自定义项。。。。。。。确保它位于标准模块中。。。。。。。。。。。。确保你像这样使用它:*=SpacedOut(B9)**。工作完美。这就是OP想要的:)你好,谢谢你的回答,我会尝试一下。你能就如何在谷歌电子表格中使用宏提出建议吗?或者我需要将结果传输到Excel然后返回到google电子表格吗?我也想知道如何在google文档中运行宏!哈哈,好吧,那我该如何在Excel 2010中实现呢?我添加了开发工具、启用脚本并添加了模块。当我使用函数时,我只得到#Name?在字段中,指示它不知道函数的名称。但它确实会出现在可用函数列表中。也许