Excel VBA:溢出错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20339067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:20:15  来源:igfitidea点击:

Excel VBA: Overflow error

excelvbaoverflow

提问by Domin1992

Just started programming in VBA, I have a problem, and i don't know how to solve this. I think everything is ok. There shows Run-Time Error '6' Overflow when i want to run this macro.

刚开始用 VBA 编程,我有一个问题,我不知道如何解决这个问题。我认为一切都很好。当我想运行这个宏时,会显示运行时错误“6”溢出。

Sub Dzia?aj()
Dim Tablica(1 To 5000) As String
Dim Dni()
Dim kolumna As Integer
Dim wiersz As Integer
Dim licznik As Integer
Dim PF As Boolean
Dim tmp As Integer
Dim i As Integer
Dim tmp2 As String
licznik = 2
tmp = 0
PF = False
kolumna = 22
wiersz = 2
Do
    If Worksheets("Pocz?tkowe").Cells(wiersz, kolumna).Value <> vbNullString Then
        For i = 1 To licznik
            If Worksheets("Pocz?tkowe").Cells(wiersz, kolumna).Value = Tablica(i) Then 'debugger shows problem here i guess
                PF = True
                tmp = i
            End If
        Next i
    End If
    If Worksheets("Pocz?tkowe").Cells(wiersz, kolumna).Value = "koniec" Then
        Exit Do
    End If
    wiersz = wiersz + 1
Loop
End Sub

Can anyone tell me where i made a mistake? I would be very grateful.

谁能告诉我我哪里出错了?我会很感激。

回答by nutsch

If you don't find the value koniecbefore row 32767, your variable wierszwill max out. If you want to continue past that, you should redefine it as Long.

如果koniec在第 32767 行之前没有找到该值,您的变量wiersz将达到最大值。如果您想继续过去,您应该将其重新定义为 Long。

You should also provide an exit for your loop, e.g. existing at the last used row. Instead of a do ... loop, I usually use the following code:

您还应该为您的循环提供一个出口,例如存在于最后使用的行中。而不是 a do ... loop,我通常使用以下代码:

Dim lLastRow As Long, lWiersz As Long

lLastRow = Cells(Rows.Count, kolumna).End(xlUp).Row

For lWiersz= 1 To lLastRow


Next lWiersz