MattE
MattE

Reputation: 1114

Excel VBA "Overflow" Error on setting Range.Value

I have a macro that runs when the Excel workbook is opened, and basically takes data from a tab named "current day" and moves it to "previous day" and then takes data from another workbook and pastes it into current day(actually uses range.value not copy/paste). Everything was working fine for multiple tests over several days. However, now it is throwing an Overflow Error on the Range.Value part of the code. This is definitely not a size issue as the code is:

PrevDay.Range("A1:Q" & ScoreCurRows).Value = CurrDay.Range("A1:Q" & ScoreCurRows).Value

Where ScoreCurRows is declared as a Long and pulls the UsedRange from the "Current Day" sheet and PrevDay is defined as Sheets("Previous Day") and CurrDay is defined as Sheets("Current Day"). It also is usually under 20 rows worth of data, so even if it was an integer, this still shouldn't cause a problem.

Does anyone have an idea why this is suddenly throwing an Overflow Error and if there is something I should be looking for?

Upvotes: 2

Views: 4770

Answers (1)

A.S.H
A.S.H

Reputation: 29332

You must have some values in the source range that caused the overflow when VBA tried to load them into a variant array in

= CurrDay.Range("A1:Q" & ScoreCurRows).Value

For example, a cell formatted as date with a value that's too large for a date. For this reason, use .Value2 instead, which will return the dates as normal numbers without trying to convert them.Besides, although most posts on SO donmt stick to this rule, it is known good practice to always use .Value2 when reading a range value (not necessarily when assigning).

PrevDay.Range("A1:Q" & ScoreCurRows).Value = CurrDay.Range("A1:Q" & ScoreCurRows).Value2
'                                                                                 ^^^^^^

Upvotes: 4

Related Questions