0

I am writting a CRC8 function in VBA which results in error: Overflow !

Since I cannot fix it and there are no sample code solutions on how to fix the overflow error with left shifting, I`d like to try and turn it off to see the result I get.

I cannot find it in options.

1
  • 2
    Change As Integer to As Long. Oct 30, 2014 at 18:08

3 Answers 3

4

I cannot find it in options.

This is because it is a runtime error, not an "option."

Most likely a cause of an Overflow error is trying to slick a number that's too huge for the data type.

Byte type is for 0-255. Integer type can hold numbers up to 32,767. Long data type can hold numbers up to 2,147,483,647 so it's probably better suitable for your specific case.

Adjust your variables types accordingly.

Generally, VBA converts all Integers to Long these days so it's a better practice to dim your variables as Long by default.


Here is an excellent writup as to why you should use them as Long.

0
3

Here is how you left shift a Byte without overflowing:

Dim abyte as Byte
abyte = &H81 '10000001

'To shift left, you multiply by 2, BUT because the msb is 1, multiplying by 2 gives you an Overflow.
'To fix that, you first mask off the leftmost bit, THEN multiply by 2...

'abyte is                              10000001
'And it with the mask &H7F         And 01111111
'which results in                    = 00000001
'multiplied by 2 is               *2 = 00000010

abyte = (&H7F And abyte) * 2
1
  • It's a bit more complicated for Integer/Long because they are signed, but it's still possible.
    – Blackhawk
    Oct 31, 2014 at 14:02
-4

Use:

On Error Resume Next ' Disable Error Handler
  .. do the calculation ..
  If Err Then 
     ' Handle Error
  End If
On Error Goto 0   ' Activate Error Handler
2
  • 3
    This won't allow OP to evaluate an expression which raises an error. This simply ignores the error and proceeds to the next line of syntax. Oct 30, 2014 at 18:09
  • 2
    gosh... that's a terrible suggestion
    – user2140173
    Oct 31, 2014 at 11:05

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.