简体   繁体   English

在VBA中打印Long to Cells数组并获取下标超出范围错误

[英]Printing Array of Long to Cells in VBA and getting subscript out of range error

I am almost done with the spreadsheet where I compared two arrays and anything that was in one array and not in another got put into a third array. 我几乎完成了电子表格的工作,在该电子表格中我比较了两个数组,将一个数组而不是另一个数组中的任何内容放入了第三数组中。

I then want to put the values in the array to cells on a sheet of the workbook, but I get a subscript out of range even though the array shows a value in the debugger. 然后,我想将数组中的值放到工作簿上的单元格中,但是即使数组在调试器中显示了一个值,我的下标也超出了范围。

Here is the loop to print the array: 这是打印数组的循环:

 If (Not MissingLoans) = -1 Then
  ThisWorkbook.Sheets("Inputs and Results").Cells(PrintCell, 1) = "No Missing Loans Found on Roll-Up"

Else
    For i = 1 To (UBound(MissingLoans())) Step 1
       *** ThisWorkbook.Sheets("Inputs and Results").Cells(PrintCell, 1).Value = MissingLoans(i)

        PrintCell = PrintCell + 1
    Next
End If

I put asterisks by the line that is giving me the out of range error, but MissingLoans(I) is showing a value. 我在出现超出范围错误的行旁加上星号,但是MissingLoans(I)显示一个值。 In fact Missingloans(1) is the only value in the array. 实际上,Missingloans(1)是数组中的唯一值。

if there is only one value in the array, then you should access it with Missingloans(0) as arrays are 0-based. 如果数组中只有一个值,则应使用Missingloans(0)访问它,因为数组是基于0的。

Try this 尝试这个

For i = LBound(MissingLoans()) To (UBound(MissingLoans())) Step 1
   ThisWorkbook.Sheets("Inputs and Results").Cells(PrintCell, 1).Value = MissingLoans(i)
   PrintCell = PrintCell + 1
Next

Rather than looping the array, assign the array directly to the sheet: 而不是循环数组,而是直接将数组分配给工作表:

Else
    Dim myRange as Range
    Set myRange = ThisWorkbook.Sheets("Inputs and Results").Cells(PrintCell, 1)

    '## Assumes Option Base 0, if Option Base 1, remove the +1
    myRange.Resize(Ubound(MissingLoans)+1).Value = Application.Transpose(MissingLoans)

    Next
End If

This code should also raise an error if the worksheet named Inputs and Results doesn't exist in ThisWorkbook , which I suspect is the real cause of your subscript out of range. 如果ThisWorkbook中不存在名为Inputs and Results的工作表,则此代码也会引发错误,我怀疑这是您的下标超出范围的真正原因。

Note: I would just use Option Base 0 , it's the default and most common by a long shot. 注意:我只会使用Option Base 0 ,这是默认设置,并且从长远来看最常见。 Just learn to live with it. 只是学习生活。 I would also begin filling the array at position 0, not 1. But if you insist on using Option Base 0 and filling it from 1..., then you'd need to do the iteration For i = 1 To UBound(MissingLoans) loop, or a ReDim Preserve on the array, either of which I think is pointlessly complicating what can be more easily done with direct array>range assignment, as per above. 我还将在位置0而不是1处开始填充数组。但是,如果您坚持使用Option Base 0 从1开始填充它,那么您需要For i = 1 To UBound(MissingLoans)进行迭代For i = 1 To UBound(MissingLoans)循环或数组上的ReDim Preserve ,我认为上述两种方法都毫无意义地使直接数组>范围分配更容易完成。

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

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