|
-
Apr 13th, 2016, 01:49 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] Bizarre Excel VBA Overflow error
I have a loop routine that loads a small rectangular range (36 x 12 cells) into a variant (array) for processing. This is done for every worksheet of every open workbook. For some reason, I get an Overflow (Run-time error '6') on a specific worksheet within a workbook and I can't figure out why.
Code:
Dim aV As Variant
aV = ws.Range("A1").Resize(36, 12) '<== Run-time error '6' Overflow: for certain ws
The routine reading the cells is called once for each worksheet (meaning aV is initialized for each worksheet). The routine might process half a dozen worksheets in other workbooks but always fails on the 2nd worksheet of a specific workbook. I only get the error if the offending workbook is open. It doesn't happen with any other workbook I've tried. When I look at the offending sheet I can't visually see anything strange in any of the cells.
Has anyone seen this behavior and/or can suggest why it is happening?
-
Apr 13th, 2016, 04:04 PM
#2
Frenzied Member
Re: Bizarre Excel VBA Overflow error
that is not enough code to really understand your problem...
why pass the range that is an array into an array in the first place
what do you need to do that needs an array?
here to help
-
Apr 13th, 2016, 04:09 PM
#3
Re: Bizarre Excel VBA Overflow error
what do you need to do that needs an array?
much faster to process an array, than keep referencing a range
on a specific worksheet within a workbook and I can't figure out why.
can you post a sample that demonstrates the problem? or email if you do not wish to post
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Apr 13th, 2016, 04:24 PM
#4
Thread Starter
Fanatic Member
Re: Bizarre Excel VBA Overflow error
I've since determined there is something about the sheet itself that's the problem. Actually, upon further investigation, there are several sheets (maybe 8 out of 20) in the same workbook that result in the error. If I successively delete each sheet that causes the overflow error, I get no errors with the remaining sheets. Before I post or email anything I'll investigate further.
I was hoping the description would trigger something with someone, but I guess not (so far anyway).
-
Apr 13th, 2016, 05:27 PM
#5
Thread Starter
Fanatic Member
Re: Bizarre Excel VBA Overflow error
Well, I've figured out what the problem is, but the workaround isn't very pleasant. I didn't know this, but when you read a cell's value with VBA, the default quantity captured is the FORMATTED value, not the raw data itself. I've since discovered that cell.Value2 is the raw data value (cell.Value is the FORMATTED value). If I process each cell within the range (instead of reading the range into a variant) and use the Value2 property, I get no error. What I found is that the data was formatted on the sheet as a date but the value was too large to be a date (thus, the overflow error) !!!!!
Therefore, the immediate workaround appears to be abandoning the preferred method of reading the range into a variant, but rather read each cell (with Value2 property instead of Value). I suppose an alternative would be to systematically reset the formatting on each sheet before reading the range, but I'd really rather not take that approach (I don't want to mess with workbooks that aren't mine).
Duh, the obvious solution is:
Code:
Dim aV As Variant
aV = Range("A1").Resize(4, 3).Value2
Last edited by VBAhack; Apr 13th, 2016 at 05:44 PM.
-
Apr 14th, 2016, 04:19 AM
#6
Re: [RESOLVED] Bizarre Excel VBA Overflow error
What I found is that the data was formatted on the sheet as a date but the value was too large to be a date
why not fix this?
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Apr 14th, 2016, 12:18 PM
#7
Thread Starter
Fanatic Member
Re: [RESOLVED] Bizarre Excel VBA Overflow error
 Originally Posted by westconn1
why not fix this?
The routine is used by others to look for open sheets that fit a certain data pattern. I have no control over what additional (non project related) sheets the user might have open, thus I have a strict rule of not modifying open sheets except for those that are determined to be project related. Actually, the error occurred on a non project related sheet. Using the Value2 property is an elegant solution.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|