vba 如何在excel vba中的字符串中添加空格?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44924127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 12:50:43  来源:igfitidea点击:

How to add space in a string in excel vba?

excel-vbavbaexcel

提问by Huwatski

how can i add space in this example string?

如何在此示例字符串中添加空格?

Input value in cell A1 is ABCDEFGHIJKand will be paste in another cell B1 with a format of ABCDE FG HIJK.

单元格 A1 中的输入值ABCDEFGHIJK将粘贴到另一个单元格 B1 中,格式为ABCDE FG HIJK.

回答by Rik Sportel

One formula to insert a space: =LEFT(A1,5)&" "&RIGHT(A1,LEN(A1)-5)to insert a space after the 5th position. (ABCDE FGHIJK)

插入空格的一个公式: =LEFT(A1,5)&" "&RIGHT(A1,LEN(A1)-5)在第 5 个位置后插入一个空格。( ABCDE FGHIJK)

One to insert 2 spaces as per example: =LEFT(A1,5)&" "&MID(A1,6,2)&" "&RIGHT(A1,LEN(A1)-7)Input ABCDEFGHIJK, result ABCDE FG HIJK

一个插入 2 个空格,例如: =LEFT(A1,5)&" "&MID(A1,6,2)&" "&RIGHT(A1,LEN(A1)-7)Input ABCDEFGHIJK, resultABCDE FG HIJK

In short: Use =LEFT(), =RIGHT()and =MID()to get parts of your string and concatenate the parts and your spaces.

简而言之:使用=LEFT(),=RIGHT()=MID()获取字符串的一部分并将部分和空格连接起来。

Edit:In VBA:

编辑:在VBA中:

Public Function StringWithSpaces(inpStr As String) As String
    StringWithSpaces = Left(inpStr, 5) & " " & Mid(inpStr, 6, 2) & " " & Right(inpStr, Len(inpStr) - 7)
End Function