5

这似乎太容易了,但我很绝望。

我需要做的是获取“D”列的最后一个值

大量的数字,例如。987654321,如果值只有两位数,代码就可以了。我只是无法确定问题所在。

Dim lastRow As Long
lastRow = Cells(Rows.Count, "D").End(xlUp).Value
Sheets("Sheet1").TxtBox1.Value = lastRow
4

2 回答 2

12

就像我在评论中提到的那样,对于这么大的数字,您必须将其声明为双精度。

Dim lastRow As Double

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

  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
    
于 2013-03-22T08:54:08.130 回答
4

Long 只能处理高达 2.1B 的值!对于任何更大的值,最好使用Double而不是Long

于 2013-03-22T08:49:28.393 回答