VBA-Excel overflow error due to long data type

It may seem too easy, but I'm so desperate.

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

a large number of numbers , for example. 987654321, the code is OK 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
+5
source share
2 answers

As I mentioned in my comment, for such a large number, you must declare it double.

Dim lastRow As Double

Alternatively, since you want to save it in a text box, you can do 2 things

  • Declare it as a string
  • Store directly in the text box.

    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
    
+12
source

2.1B! Double Long

+4

All Articles