【问题标题】:Excel VBA array not populating from loopExcel VBA数组未从循环中填充
【发布时间】:2015-05-26 15:27:15
【问题描述】:

这里是代码。我要做的是创建一个数组,其中包含存在一些空格的字符串(名为“difficultstring”)的所有索引位置(字符串看起来像“text text text ...”):

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) 等时,什么都没有显示。

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

【问题讨论】:

  • 考虑使用 Preserve 选项
  • ReDim 更改为Redim Preserve

标签: arrays vba excel basic


【解决方案1】:

您需要在您的 ReDim 语句中使用“Preserve”命令,就像引用的 @Gary 的学生一样。试试:

ReDim Preserve cutarray(UBound(cutarray) + 1)

【讨论】:

    猜你喜欢
    • 2021-04-21
    • 1970-01-01
    • 2016-06-28
    • 1970-01-01
    • 1970-01-01
    • 2011-10-08
    • 1970-01-01
    • 2011-08-18
    • 1970-01-01
    相关资源
    最近更新 更多