Excel VBA:溢出错误 [英] Excel VBA: Overflow error

查看:641
本文介绍了Excel VBA:溢出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚开始在VBA中编程,我有一个问题,我不知道如何解决.我认为一切都很好.当我要运行此宏时,显示运行时错误"6"溢出.

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.

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.

推荐答案

如果在第32767行之前找不到值koniec,则变量wiersz将最大.如果要继续下去,则应将其重新定义为Long.

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

您还应该为循环提供出口,例如存在于最后使用的行.我通常使用以下代码代替do ... loop:

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:

Dim lLastRow As Long, lWiersz As Long

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

For lWiersz= 1 To lLastRow


Next lWiersz

这篇关于Excel VBA:溢出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆