由于数据类型过长而导致VBA-Excel溢出错误 [英] VBA-Excel Overflow Error due to Long data type

查看:60
本文介绍了由于数据类型过长而导致VBA-Excel溢出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎太简单了,但是我非常绝望.

This may seem too easy, but I am so desperate.

我需要做的是获取"D"列的最后一个值,该列具有

What I need to do is get the last value of the column "D" which has a

大量号码,例如987654321,如果值只有两位数字,则代码是正确的.我只是无法确定问题所在.

big amount of number, ex. 987654321, the code is fine if the value is only two-digit. I just can't identify the problem.

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

就像我在评论中提到的那样,

推荐答案

对于如此大的数字,您必须将其声明为double.

Like I mentioned in my comment, for such large number you have to declare it as a double.

Dim lastRow As Double

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

Alternatively since you want to store it in a textbox you can do 2 things

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

  1. Declare it as a string
  2. Store it directly in the Textbox.

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

这篇关于由于数据类型过长而导致VBA-Excel溢出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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