由于长数据类型导致的VBA-Excel溢出错误

Kry*_*nce 5 excel vba excel-vba

这似乎太容易了,但我非常绝望.

我需要做的是得到列"D" 的最后一个值,它有一个

大量的数字,例如 987654321,如果该值仅为两位数,则代码正常.我只是无法确定问题.

Dim lastRow As Long
lastRow = Cells(Rows.Count, "D").End(xlUp).Value
Sheets("Sheet1").TxtBox1.Value = lastRow
Run Code Online (Sandbox Code Playgroud)

Sid*_*out 12

就像我在评论中提到的那样,对于如此大的数字,你必须将它声明为双精度数.

Dim lastRow As Double
Run Code Online (Sandbox Code Playgroud)

或者,因为您想将其存储在文本框中,您可以做两件事

  1. 将其声明为字符串
  2. 将其直接存储在文本框中.

    Option Explicit
    
    Sub Sample1()
        Dim lastRow As String
    
        With Sheets("Sheet1")
            lastRow = .Cells(.Rows.Count, "D").End(xlUp).Value
            .TextBox1.Value = lastRow
        End With
    End Sub
    
    Sub Sample2()
        With Sheets("Sheet1")
            .TextBox1.Value = .Cells(.Rows.Count, "D").End(xlUp).Value
        End With
    End Sub
    
    Run Code Online (Sandbox Code Playgroud)