VBA 宏运行时错误 6:循环内溢出编码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12588485/
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 14:04:31  来源:igfitidea点击:

VBA Macro Run time error 6: overflow- coding inside a loop

vbaexcel-vbaexcel

提问by John Hopley

Having a problem with this Error. I am creating a GA and the loop is to assign my fitness value to an array.

遇到此错误的问题。我正在创建一个 GA,循环是将我的适应度值分配给一个数组。

some of the variables

一些变量

Dim Chromolength as integer
Chromolength = varchromolength * aVariables 
Dim i as integer, j as integer, counter as integer
Dim Poparr() As Integer
Dim FitValarr() As Integer

the code:

编码:

ReDim Poparr(1 To PopSize, 1 To Chromolength)

For i = 1 To PopSize                
  For j = 1 To Chromolength       
    If Rnd < 0.5 Then           
        Poparr(i, j) = 0
    Else
        Poparr(i, j) = 1        
    End If
  Next j
Next i

For i = 1 To PopSize                   
 j = 1                           
 counter = Chromolength              
 Do While counter > 0  
   FitValarr(i) = FitValarr(i) + Poparr(i, counter) * 2 ^ (j - 1)          
  j = j + 1                   
  counter = counter - 1       
 Loop
Next i      

I am having problems with:

我有以下问题:

FitValarr(i) = FitValarr(i) + Poparr(i, counter) * 2 ^ (j - 1) 

I apologize, I am fairly new to VBA.

我很抱歉,我对 VBA 相当陌生。

回答by David W

An overflow condition arises when you create an integer expression that evaluates to a value larger than can be expressed in a 16-bit signed integer. Given the expression, either the contents of FitValarr(i), or the expression 2^(j-1) could be overflowing. Suggest all the the variables presently declared as Int be changed to Long. Long integers are 32-bit signed values and provide a correspondingly larger range of possible values.

当您创建的整数表达式的计算结果大于可以用 16 位有符号整数表示的值时,就会出现溢出情况。给定表达式,FitValarr(i) 的内容或表达式 2^(j-1) 可能溢出。建议将当前声明为 Int 的所有变量更改为 Long。长整数是 32 位有符号值,并提供相应更大范围的可能值。

回答by tonyec99

I had the same run time error 6. After much investigation l discovered that mine was a simple 'divide by zero' error.

我有同样的运行时错误 6。经过大量调查后,我发现我的是一个简单的“除以零”错误。

回答by Paul1307

I set up an integer value to hold Zip codes, and Error 6 events plagued me - until I realized that a zip code of 85338 exceeded the capacity of an int... While I didn't think of a zip code as a "value" it was nonetheless certainly interpreted as one. I suspect the same could happen with addresses as well as other "non-numeric" numeric values. Changing the variable to a string resolved the problem. It just didn't occur to me that a zip code was a "numeric value." Lesson learned.

我设置了一个整数值来保存邮政编码,错误 6 事件一直困扰着我 - 直到我意识到 85338 的邮政编码超出了 int 的容量......虽然我不认为邮政编码是一个“值” “尽管如此,它肯定被解释为一个。我怀疑地址和其他“非数字”数值也会发生同样的情况。将变量更改为字符串解决了问题。我只是没有想到邮政编码是一个“数值”。学过的知识。