容量溢出 - excel vba [duplicate]

Capacity overflow - excel vba [duplicate]

提问人:gibbs44 提问时间:7/13/2023 最后编辑:Jonathan Willcockgibbs44 更新时间:7/13/2023 访问量:27

问:

我正在编写一个函数来比较两个日期。我的问题是粗体线。 当代码执行时,我收到溢出错误。在互联网上进行了几次搜索后,我了解到使用interger而不是longger时可能会发生这种类型的错误,这不是我的情况。

我有大约 23.5 万行要处理,我有点急于在月底之前完成这个函数的编程。

有谁知道为什么会发生这个错误?

这是我的代码:

Public Function date_dif(sheet As String, number As Integer, colonnetarget As Integer, colonnedate1 As Integer, Optional colonnedate2 As Integer)

Dim last_row As Long
Dim sht, sht2 As Worksheet
Dim ligne As Integer
Dim formule As String
Dim crit1, crit2 As String

Set sht = ThisWorkbook.Worksheets(sheet)

last_row = sht.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious, LookIn:=xlValues).Row

If colonnedate2 <> 0 Then
    **For ligne = 2 To last_row**
        sht.Cells(ligne, colonnetarget).Value = DateDiff("d", sht.Cells(ligne, colonnedate1).Value, sht.Cells(ligne, colonnedate2).Value)
    Next
Else
    For ligne = number To last_row
        sht.Cells(lignxe, colonnetarget).Value = DateDiff("d", sht.Cells(ligne, colonnedate1).Value, Now())
    Next
End If


End Function
Excel VBA 函数 日期

评论

1赞 Jonathan Willcock 7/13/2023
last_row 是 Long,但 ligne 是整数。如果last_row大于 Integer,则将得到 Overflow。将 ligne 也更改为 Long。我投票决定以错别字结束
0赞 FunThomas 7/13/2023
只需始终在 VBA 中使用。 定义为 2 个字节,因此最大值为 32768。在 32 位(或 64 位)系统上,它占用 4 个字节,VBA 在运行时检查限制(以保持兼容性),因此您无论如何都不会节省内存空间 - 请参阅 stackoverflow.com/a/47024456/7599798LongInteger

答:

1赞 Dominique 7/13/2023 #1

以下内容在代码中没有意义:

Dim last_row As Long
...
Dim ligne As Integer
...
    **For ligne = 2 To last_row**

应该是这样,计数器也应该如此,一直到那个:-)last_rowLongLong

评论

1赞 gibbs44 7/13/2023
感谢您的所有评论,我的代码现在可以完美运行。=)