我正在使用 add_serial_number VBA 代码,但出现了溢出错误。

huangapple go评论54阅读模式
英文:

I am using add_serial_number VbA code but is getting overflow error

问题

在我的代码下面给出了。

我只有2万行。
我正在使用长数据类型,但不知道为什么会出现运行时错误。

Option Explicit

Sub add_serial_num()

    Dim i As Long, lr As Long

    lr = Cells(Rows.Count, "B").End(xlUp).Row

    For i = 2 To lr

        If Cells(i, "B").Value <> "" Then
            Cells(i, "A").Value = i - 1
        End If

    Next i

End Sub

出现溢出运行时错误
我已经使用了长数据类型但不知道为什么会出现这个错误
我只有2万行
我正在使用长数据类型但不知道为什么会出现运行时错误
英文:

Below my code is given.

I have only 20K rows.
I am using long data type but don't know why i am getting run time error.

Option Explicit

Sub add_serial_num()
    
    Dim i As Long, lr As Long
    
    lr = Cells(Rows.Count, &quot;B&quot;).End(xlUp).Row
    
    For i = 2 To lr
        
        If Cells(i, &quot;B&quot;).Value &lt;&gt; &quot;&quot; Then
            Cells(i, &quot;A&quot;).Value = i - 1
            
        End If
                    
    Next i
            
End Sub

Getting run time error of overflow.
I have used long data type. But i don't know why i am getting this error.
I have only 20K rows.
I am using long data type but don't know why i am getting run time error.

答案1

得分: 1

这可能发生在列 B 中的某个单元格之一应用了格式,但值与格式不匹配。例如,如果一个单元格格式化为日期,但其值为1,000,000,000。

检查代码停止的行的格式和值。

另一个选项是使用.Value2而不是.Value,这将忽略格式。

英文:

This can happen if one of the cells in column B has a format applied to it, but the value doesn't match the format. For example if a cell is formatted as date, but the value is 1,000,000,000.

Check the format and value of whatever row the code stops on.

Another option is to use .Value2 instead of .Value, this will ignore the formatting.

huangapple
  • 本文由 发表于 2023年7月17日 13:49:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76701781.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定