shwq.net
当前位置:首页 >> VBA按指定标示符分割字符串问题 >>

VBA按指定标示符分割字符串问题

Private Function MyFunction() Dim s As String, Arr s = "1,2,3,4,5,6,7,8,9,0" Arr = Split(s, ",") '以逗号分割文本并放入数组,注意这个数组定义时候不能带括号, For i = 0 To UBound(Arr) MsgBox Arr(i) Next End Function

Private Function MyFunction() Dim s As String, Arr s = "1,2,3,4,5,6,7,8,9,0" Arr = Split(s, ",") '以逗号分割文本并放入数组,注意这个数组定义时候不能带括号, For i = 0 To UBound(Arr) MsgBox Arr(i) Next End Function

Dim A A = Split("how are you", " ") 执行以上赋值之后: A(0)="how",A(1)="are",A(2)="you"

可以用 Split() 进行分割 Sub SplitExample() Dim Str, Val, n Str = "资产分类-->硬件类-->整机-->个人处理设备-->笔记本-->中端笔记本" Val = Split(Str, "-->") For n = LBound(Val) To UBound(Val) MsgBox Val(n) Next End Sub

试了一下,没有分隔符,会返回原来的字符串: a = "abc|de|fgh" b = Split(a, "|") c = Split(a, ",") b会把a拆成三个: b(0)="abc",b(1)="de", b(2)="fgh" c的值会等于a: c(0) = "abc|de|fgh"

答:数组的合并和字符串拆分(Join & Split):多个字符的合并和字符串按规律的拆分是经常遇到的,如: A-REW-E-RWC-2-RWC 按分隔符-拆分成6个字符放在一个数组中 有一组数array(23,45,7,1,76)想用分隔符-连接成一个字符串 上面两种情况VBA提供...

用以下代码 Sub abc() For n = 1 To ActiveSheet.UsedRange.Rows.Count da = Range("A" & n).Value For m = 1 To Len(da) Cells(n, m + 1).Value = Mid(da, m, 1) Next m Next n End Sub

在A列输入你的原始数据 B列输出结果 Sub test5() Dim i As Integer Dim v i = 1 Do v = Split(Cells(i, 1), "-") Cells(i, 2) = v(1) i = i + 1 Loop Until Cells(i, 1) = "" End Sub

msgbox ab(1)应该会是下标越界吧。 因为按照-分割出来的数据应该还是只有一个ABCDE.而数组的下标是从0开始的。所以应该是msgbox ab(0)

可以用正则把多个空格变成一个后,在split dim reg,str as stringstr="a b c d"set reg=createobject("vbscript.regexp")reg.pattern="\s+"msgbox reg.replace(str," ")'arr=split(reg.replace(str," ")," ")

网站首页 | 网站地图
All rights reserved Powered by www.shwq.net
copyright ©right 2010-2021。
内容来自网络,如有侵犯请联系客服。zhit325@qq.com