0

I have a pretty simple question. I wrote this code to look for a preexisting date so that I can replace it. This code has worked perfectly, but I created a copy of the file to run the same code with a separate data set and now the code gets hung up on the fourth line giving me an overflow error. Any idea as to what the root of the problem is?

Dim i As Integer, ValueToFind As Integer, LRow As Integer
intValueToFind = Sheet8.Range("K6")
Sheet3.Activate
LRow = Range("I" & Rows.Count).End(xlUp).Row
For i = 1 To LRow
If Cells(i, 9).Value = intValueToFind Then
MsgBox ("Found value on row " & i)
3
  • 5
    1. Change all Integer to Long, 2. Forget you ever heard the word integer. Commented Jun 22, 2018 at 18:09
  • @jeeped awesome thank you, that worked perfectly. Just so I can understand, do you have an idea why it would work for one file and not another? Commented Jun 22, 2018 at 18:20
  • 3
    Because one worksheet has more rows than the maximum for a signed short integer (32,767) but neither had more rows than the maximum of a signed long integer (2,147,483,647). google-fu Commented Jun 22, 2018 at 18:25

1 Answer 1

2

Looks like Range("I" & Rows.Count).End(xlUp).Row is returning a value that's larger than an Integer can fit.

Integer is a 16-bit signed integer data type, which means its maximum positive value is 32,767, 2^15-1.

A worksheet can have many, many, many times more rows than that.

Use a Long instead, a 32-bit signed integer data type, with a maximum positive value of 2,147,483,647, 2^31-1.

Don't worry about allocating 32 bits for every integer - on modern computers things are optimized for Int32 anyway; you hardly ever need to use a 16-bit integer type.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.