簡體   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