【问题标题】:Excel VBA Run-time Error '6' when populating an array填充数组时 Excel VBA 运行时错误“6”
【发布时间】:2016-02-09 21:29:47
【问题描述】:

我已经解决了这个问题。问题与代码无关(直接)。这与 lRow 变量有关。这个值是由我的电子表格上的一个公式填充的,而这又是由另一个宏填充的。我发现我插入了一列,因此更新了错误的列。我更改了代码以更新命名范围而不是实际地址,问题就消失了。这段代码失败仍然有点奇怪,因为第 24 行中的值对我来说似乎是有效的。

感谢大家的意见!


希望有人可以在这里提供帮助。我最近在尝试运行我的代码时开始遇到错误。

这个错误出现在这行代码:

NewArr = Sheets("Input Sheet").Range("A" & lRow & ":BJ" & lRow).Value

上面的变量声明为:

Dim NewArr() As Variant

lRow 评估为 24,因此想法是名为 NewArr 的数组将填充出现在输入表范围 A24:BJ24 上的值。

我知道变体数据类型可以处理非常大的数字,虽然这一行中有一些大数字和文本等,但没有一个超过 400 万。 (一个是 3380438.98545603)。关于我为什么会收到此错误的任何想法?

TIA!

我的完整代码(直到出现错误)是:

Private Sub BT_SUBMIT_Click()

Dim NewArr As Variant
Dim lRow As Long
Dim lCol As Long
Dim PasteRange As Range
Dim bUpdated As Boolean

'Check if the user  has ticked the confirm box before writing to the spreadsheet
If Me.XB_CONFIRM.Value = False Then
    MsgBox "You must tick the confirm box to indicate you understand that " _
    & "your amendments cannot be undone.  Please return to the form, tick the box and " _
    & "resubmit.", vbOKOnly, "Please tick confirmation box"
    Exit Sub
End If

'Check if the user has selected either yes or no on changes frame
If Me.OB_CHNGS_NO.Value = False And Me.OB_CHNGS_YES.Value = False Then
    MsgBox "You must select either yes or no to indicate if there are changes being made." _
    & "  Please correct and resubmit.", vbCritical, "Select Yes or No"
    Exit Sub
End If

'Check if the Target Resolution Date is at least today or later and if not
'user must update
If Sheets("Input Sheet").Cells(Sheets("Lookups").Range("NEW_IAR_ROW").Value, 38).Value < Now() And Me.OB_CHNGS_YES.Value = False Then

    MsgBox "The Target Resolution Date must be no earlier than today." _
    & "  Please correct and resubmit.", vbCritical, "Change Target Resolution Date"

    Me.OB_CHNGS_YES.Value = True
    Exit Sub
End If


'Check if the values enterered are valid
If Me.OB_CHNGS_YES Then
    If Control_Error("FR_AMNDMENTS") Then
        Exit Sub
    End If

    If (Len(Me.TB_AMNT_RES.Value) + Len(Me.TB_NO_TRNS_RES.Value) + Len(Me.TB_CMNT_RES.Value)) > 0 Then
        bUpdated = True
        If Control_Error("FR_IAR_RSVD") Then
            Exit Sub
        End If
    End If
    If (Len(Me.TB_AMNT_WO.Value) + Len(Me.TB_NO_TRNS_WO.Value) + Len(Me.TB_CMNT_WO.Value)) Then
        bUpdated = True
        If Control_Error("FR_IAR_WO") Then
            Exit Sub
        End If
    End If

    If (Len(Me.TB_AMNT_INC.Value) + Len(Me.TB_NO_TRNS_INC.Value) + Len(Me.TB_CMNT_INC.Value)) Then
        bUpdated = True
        If Control_Error("FR_IAR_INCR") Then
            Exit Sub
        End If
    End If

    If Not (bUpdated) Then
        MsgBox "You have not entered any changes for resolved, write offs or increases.  " _
        & "You must update at least one of these sections or select no changes.", vbCritical, "Error"
        Exit Sub
    End If
End If

lRow = Sheets("Lookups").Range("NEW_IAR_ROW").Value
lCol = Sheets("Lookups").Range("TOTAL_COLUMNS").Value

'First we load the array with the current values on the sheet.
NewArr = Sheets("Input Sheet").Range("A" & lRow & ":BJ" & lRow).Value

Application.ScreenUpdating = False
'Then there is other code beyond this point
End Sub

【问题讨论】:

  • 你能在newArr= 行的前面和后面直接显示该行吗?理论上不会在该行的那部分发生错误...
  • 问题实际上不是数字的大小,而是数字用于存储在内存中的位数。话虽如此,您引用的数字仅使用 22 位内存,因此应该不是问题。
  • 您是否尝试将变量声明为Dim NewArr As Variant?不是数组。
  • 您可以尝试将数组调整为加载范围所需的大小,然后循环遍历范围,将数据逐个元素复制到数组中。这样你就可以知道问题出在哪里(假设问题实际上是在那条线上,而不是像 Dirk 建议的那样在相邻的线上)。
  • 我尝试了多种方法来重现您的问题,但即使是大随机数也不能。您可能需要发布一些实际数据。所以强烈推荐minimal, complete, and verifiable example

标签: arrays excel vba


【解决方案1】:

由于我没有足够的代表发表评论,所以我决定写这个作为答案。

我遇到了和你一样的问题,找到了两个解决方案。

我尝试了相关帖子 (Excel VBA load range into array overflow error) 的这个答案,它奏效了。 问题是我有该项目的以前(功能)版本,所以当我复制和粘贴代码时,我使用了功能(以前)版本中的代码。

或者,您可以尝试使用 .Value2 代替 .Value。 那是另一件事,它带来了额外的好处,即不必创建新模块来复制/粘贴代码(或对项目进行任何版本控制)。

希望对遇到这个问题的人有所帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多