为变种崩溃分配值,为什么?

时间:2016-09-13 15:38:53

标签: vba excel-vba excel

我在这里有这个可爱的代码,它通过选择文件夹来处理大量文件,并且必须将内容收集到新工作表中。在这部分中,我想将标题数据存储到数组中,因为它是静态的,必须附加到每一行。标题信息来自cellPosition数组,其中包含像B2这样的单元格值。在for循环中我得到错误save_fix_gen(i)=崩溃一些如何在第二个元素(i = 2),我无法弄清楚为什么。任何帮助都会受到赞赏!

FYI

(
    Dim save_fix_gen() As Variant, cellPositions() As Variant, i As Integer
    cellPositions() = get_general 'this comes from a function and
)
    Do While myFile <> ""

    Set ActualWorkBook = Workbooks.Open(myPath & myFile)

    ReDim save_fix_gen(0)
    ActualWorkBook.Worksheets("SheetName").Activate
    For i = LBound(cellPositions) To UBound(cellPositions)
        save_fix_gen(i) = CStr(Evaluate(cellPositions(i)))
        i = i + 1
        ReDim Preserve save_fix_gen(i)
    Next

    Loop

1 个答案:

答案 0 :(得分:0)

这很有效,谢谢。只需将数组设置为输入的最大大小即可。

ReDim save_fix_gen(UBound(cellPositions))
ActualWBK.Worksheets("Bid Leveling Template").Activate
For i = LBound(cellPositions) To UBound(cellPositions)
    save_fix_gen(i) = CStr(Evaluate(cellPositions(i)))
    i = i + 1
    'ReDim Preserve save_fix_gen(i)
Next