繁体   English   中英

Excel VBA数组未从循环填充

[英]Excel VBA array not populating from loop

这是代码。 我正在尝试做的是创建一个包含所有空格(该字符串看起来像“ text text text ...”的字符串)的索引位置的数组(名为“ difficultstring”):

Dim cutarray
cutarray = Array(0)

For spacething = 2 To Len(difficultstring)
    If Mid(difficultstring, spacething, 1) = " " And Mid(difficultstring, spacething - 1, 1) <> " " Then
    ReDim cutarray(UBound(cutarray) + 1)
    cutarray(UBound(cutarray)) = spacething
End If
Next spacething

但是由于某种原因,当我稍后尝试使用cutarray数组执行某项操作或显示来自cutarray的值时,似乎其中没有任何数字。 我检查了len(cutstring)并返回了43的值-这意味着Excel尝试填充此字符串,但实际上没有填充任何数字(当我提取cutstring(0),cutstring(1)等时,什么都没有显示。)

有人可以帮忙吗? 非常感谢!!!!!!!

您需要在ReDim语句上使用“保留”命令,例如引用@Gary的Student。 尝试:

ReDim Preserve cutarray(UBound(cutarray) + 1)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM