基于 VBA 2010 中的字符中断字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16099651/
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
Break string based on a character in VBA 2010
提问by user2021539
In Excel 2010, using VBA, how can I breakapart a string when it finds a certain character?
在 Excel 2010 中,使用 VBA,如何在找到某个字符时拆分字符串?
Let say A1
= "This is a | test of | the | emergency broadcast signal"
And I assign that to a variable like
让我们说A1
="This is a | test of | the | emergency broadcast signal"
我把它分配给一个变量,比如
strColumnA = Range("A" & CStr(currRow)).Value
Now I want to append 4 new rows at the end of worksheet 2. All Column A, like:
现在我想在工作表 2 的末尾追加 4 个新行。所有 A 列,例如:
A1 = "This is a"
A2 = "test of"
A3 = "the"
A4 = "emergency broadcast signal"
Any ideas?
有任何想法吗?
回答by glh
Use this as there is no need for a loop, also it is important to leave the Application.Trim()
in:
使用它,因为不需要循环,也很重要的是留下Application.Trim()
in:
Sub test()
Dim r As Variant, s As String
s = [a1].Value
r = Split(Application.Trim(s), "|")
[b1].Resize(UBound(r, 1) + 1) = Application.Transpose(r)
End Sub
回答by Siddharth Rout
Use Split()
用 Split()
Sub Sample()
Dim Ret
Dim strColumnA As String
Dim i As Long
strColumnA = "This is a | test of | the | emergency broadcast signal"
Ret = Split(strColumnA, "|")
For i = LBound(Ret) To UBound(Ret)
Debug.Print Ret(i)
Next i
End Sub