excelvbachecksumcrc

CRC8 calculation for Excel VBA


I'm breaking up my head with the CRC8 calculation using Excel VBA. I've written a function in VBA, which returns CRC8 value which can be later on stored into a cell. However, while printing same I'm getting an Error Saying "OverFlow".

I get overflow in the function at "ShiftLeft = Num * (2 ^ Places)"

Function CRCPrateek(CRCrng As Range) As Integer

    Dim CRC As Integer
    Dim length As Integer
    Dim Hexbyte As Integer
    Dim i As Integer

    'Initial CRC seed is Zero CRC = H00
    'The real part of the CRC. Where I commented "Polynomial", it used to be a # define
    'Polynomial 7. 'I replaced the word Polynomial with 7, however that means the 7 may
    'be subject to change depending on the version of the crc you are running.
    'To loop it for each cell in the range

    For Each cel In CRCrng
        'Verify if there is atleast one cell to work on
        If Len(cel) > 0 Then
            Hexbyte = cel.Value
            CRC = CRC Xor Hexbyte
            For i = 0 To 7
                If Not CRC And H80 Then
                    CRC = ShiftLeft(CRC, 1)
                    CRC = CRC Xor 7
                Else
                    CRC = ShiftLeft(CRC, 1)
                End If
             Next
        End If
    Next
    CRCPrateek = CRC
End Function

Function ShiftLeft(Num As Integer, Places As Integer) As Integer

    ShiftLeft = Num * (2 ^ Places)

End Function

Solution

  • Others' efforts & suggestions helped me find the correct answer; I'm posting the generic function I wrote to calculate CRC8. It gives me the desired results & I've also checked it against other CRC calculators.

    'GENERATE THE CRC 
    
    Function CRCPrateek(ByVal crcrng As Range) As Long
    Dim crc As Byte
    Dim length As Byte
    Dim Hexbyte As String
    Dim DecByte As Byte
    Dim i As Byte
    
    ' Initial CRC seed is Zero
    crc = &H0
    
    'The real part of the CRC. Where I commented "Polynomial", it used to be a # define
    'Polynomial 7. I replaced the word Polynomial with 7, however that means the 7 may
    'be subject to change depending on the version of the crc you are running.
    
    'To loop it for each cell in the range
    For Each cel In crcrng
        'Verify if there is atleast one cell to work on
     '  If Len(cel) > 0 Then
           DecByte = cel.Value
           crc = crc Xor DecByte
           For i = 0 To 7
                If ((crc And &H80) <> 0) Then
                    crc = ShiftLeft(crc, 1)
                    crc = crc Xor 7
                Else
                    crc = ShiftLeft(crc, 1)
                End If
           Next
      ' End If
    Next
    
    CRCPrateek = crc
    End Function    
    
    Function ShiftLeft(ByVal Num As Byte, ByVal Places As Byte) As Byte
    ShiftLeft = ((Num * (2 ^ Places)) And &HFF)
    End Function
    
    'END OF CRC 
    

    The only thing you got to pass here as parameter while calling the above function is the range of the cells ( which has the decimal(use HEX2DEC in cells) values.

    'EXAMPLE CALL TO CRC FUNCTION FROM A SUB
    
    'select the crc Range
    Set crcrng = Range("I88", "U88")
    crc = CRCPrateek(crcrng)
    Sheet1.Cells(88, 22).Value = crc
    MsgBox ("CRC value is " & Sheet1.Cells(86, 22).Value & "(in HEX) ")
    

    Note: This function takes the input values as decimals, calculates CRC value in decimal & later on once the CRC value is returned, you can store it in any other cells & convert back to hex by using formula DEC2HEX in cells