'How do I debug an overflow error 6 on a date?
I'm trying to do some date comparison and manipulation. My code works fine, however, randomly I'm getting an overflow 6 error. Then, if I restart Excel it works fine again.
I've tried these two options to try to solve this issue:
- To type variables as
Date, and fetch the data from a cell formatted usingRange.Value2 - To type the variables as
String, and fetch the data from a cell formatted usingRange. Value, then convert the strings into variables typed as Date usingDateValue(string)
Here's the relevant code.
Dim lastInvoiceDateInput, priorDateInput, focusDateInput As String
Dim lastInvoiceDate, priorDate, focusDate As Date
priorDateInput = Range("R1").Value
focusDateInput = Range("S1").Value
For i = 2 To lastInvoiceRow
lastInvoiceDateInput = Range("L" & i).Value
priorDate = DateValue(priorDateInput)
focusDate = DateValue(focusDateInput)
lastInvoiceDate = DateValue(lastInvoiceDateInput)
If lastInvoiceDate > priorDate And lastInvoiceDate <= focusDate Then
Debug.Print ("Match")
[...]
The overflow 6 error occurs on line 9.
Another option to solve this I have tried is to use CDate instead of DateValue but still get the overflow at the same place, even though IsDate comes back True for all the data variables.
Incidentally, I tried to use IsDate on the data variables to check. It returns True for all three date variables.
I'm new to VBA and the forum. I'd appreciate any advice.
---- Edit ----
Some further progress and a possible solution.
It turns out that when you run the program step by step in debug mode, it doesn't throw the error, whereas the same program running on the same data set in real-time does. That's a mystery. It's almost as if it launches some parallel task that doesn't have time to complete.
Another thing I tried that seems to work, although I can't explain and am still hopeful of some feedback, is using Variant data types instead of Date data type. I guess it adapts the variable to the value assigned, thus avoiding the overflow error.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
