简体   繁体   English

从 For Each function VBA 打印数组结果得到下标超出范围错误

[英]Print Array result from For Each function VBA getting subscript out of range error

I continuously receive a subscript out of range error on the following and I can't understand why.我不断收到下面的下标超出范围错误,我不明白为什么。 I'm no VBA guru, I know how I would do this with JS, but I make my way VBA through projects when I have to.我不是 VBA 大师,我知道如何使用 JS 来做到这一点,但是当我不得不做的时候,我会通过项目来完成 VBA。 In this case, I need to create an array from my for each statement and print it to a file.在这种情况下,我需要为每个语句创建一个数组并将其打印到文件中。 I would just leave out the array and print from within the for each loop (which works) but I need to be able to add to the contents of the file and then also access the array later from a different function (a question for another time).我只是省略了数组并从 for each 循环中打印(这有效),但我需要能够添加到文件的内容中,然后还可以稍后从不同的 function 访问数组(另一个问题)。

Hopefully someone can help with what I am doing wrong here.希望有人可以帮助我在这里做错了什么。

Public Function Orders()
    Dim ItemDescription As String, Counter As Integer, Range As Range, sArray() As Variant
    ReDim sArray(1 To 1) As Variant
    length = Cells(Rows.Count, 1).End(xlUp).Row
    Set Range = ActiveSheet.Range("A2:A" & length)
    IntFile = FreeFile
    StrFile = "C:\Projects\Test.txt"
    Open StrFile For Output As #IntFile
    For Each Cell In Range
        ItemDescription = Cell.Offset(0, 19).Value
            If (Cell.Value = Cell.Offset(1, 0).Value And Cell.Value <> Cell.Offset(-1, 0).Value) Or (Cell.Value <> Cell.Offset(-1, 0).Value And Cell.Value <> Cell.Offset(1, 0).Value) Then
                Counter = 1
            ElseIf (Cell.Value = Cell.Offset(1, 0).Value And Cell.Value = Cell.Offset(-1, 0).Value) Or (Cell.Value <> Cell.Offset(1, 0).Value And Cell.Value = Cell.Offset(-1, 0).Value) Then
                Counter = Counter + 1
            End If
            If Counter = 1 Then
                OlStr = ItemDescription
            ElseIf Counter > 1 Then
                 OlStr = ItemDescription & " " & "your count =" & " " & Counter
            End If
            sArray(UBound(sArray)) = OlStr
            ReDim Preserve sArray(1 To UBound(sArray) + 1) As Variant
    Next Cell
    Print #IntFile, sArray(1, UBound(sArray))
    Close #IntFile
End Function

The issue occurs at the "Print #IntFile, sArray(1, UBound(sArray))" line, 3rd from the bottom.问题出现在“Print #IntFile, sArray(1, UBound(sArray))”行,倒数第三行。 I understand that this means I am trying to reference an item outside the dimension of the array, but I can't seem to figure out why.我知道这意味着我试图引用数组维度之外的项目,但我似乎无法弄清楚为什么。 If I put the Print line inside the For Each I still get the error.如果我将 Print 行放在 For Each 中,我仍然会收到错误消息。 Here's the error:这是错误:

在此处输入图像描述

As a note, it looks like the UBound is getting created correctly.请注意,看起来 UBound 正在正确创建。 I have 2537 lines in my worksheet, though it does have a header.我的工作表中有 2537 行,尽管它确实有 header。

在此处输入图像描述

Any help is greatly appreciated!!!任何帮助是极大的赞赏!!!

As per my comments:根据我的评论:

You have a one dimensional array so you can only specify one index value.您有一个一维数组,因此您只能指定一个索引值。 sArray(1) and sArray(UBound(sArray)) would be valid but not sArray(1, UBound(sArray)) sArray(1)sArray(UBound(sArray))将是有效的,但不是sArray(1, UBound(sArray))

If you just want to print all the cell values, you could put Print #IntFile, sArray(UBound(sArray)) inside the For Each loop before the Redim Statement.如果您只想打印所有单元格值,可以将Print #IntFile, sArray(UBound(sArray))放在 Redim 语句之前的 For Each 循环中。 Or, you could just bypass the array an do Print #IntFile, OlStr .或者,您可以绕过数组并执行Print #IntFile, OlStr

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

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