Newbie needs help to perform a checksum based on XOR

Nullplan

New Member
Joined
Apr 20, 2003
Messages
3
Hi, being a total newbie to excel stuff I hope I will understand your replies to my problem.

However, I'd like to calculate a checksum for a row of HEX-bytes based on XOR. Result should be in hex aswell !

Example:

aa XOR bb XOR cc XOR dd ...

Windows' own calculator will do the trick but with a growing number of bytes to 'xor', I tend to produce typos. Not to mention how boring that is... *g*

BEST would be something like a function: XOR (A1:A200) :)
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
hex XOR checksum

Hi Nullplan,

Here's a user-defined function (UDF) for Excel to add a hex XOR checksum capability. First the code:

Function ChkSumXOR(Rin As Range) As String
Dim Cell As Range
ChkSumXOR = 0
For Each Cell In Rin
If Not IsEmpty(Cell) Then
If Len(Cell) < 3 Then
ChkSumXOR = dec2hex(hex2dec(ChkSumXOR) XOR hex2dec(Cell))
Else
ChkSumXOR = 0
MsgBox "Error, cell " & Cell.Address & " contains multi-byte value", _
vbError, "Checksum Error"
End If
End If
Next Cell
If Len(ChkSumXOR) = 1 Then ChkSumXOR = "0" & ChkSumXOR
End Function


Second, its use is as you described:

=ChkSumXOR(A1:A200)


Third, if you haven't already, you must load the Analysis Toolpak add-ins (Tools > Add-Ins > select Anaysis Toolpak and Analysis Toolpak-VBA. It is also a good idea (but not essential) to load the Analysis Toolpak VBA references in the VBE (Tools > References > check atpvbaen.xls).


Finally, if you are not familiar with how to install a VBA macro or UDF,
it's really quite easy. Just follow these steps:

1) Go to the Visual Basic Editor (VBE). Do this from Tools >
Macro > Visual Basic Editor (or simply keyboard Alt-TMV)

2) In the VBE create a new Macro Module: Insert > Module.
An empty code window pane will appear (Alt-IM).

3) Paste the code into this window. The macro or function is
now available for use from Excel. If it is a Function-type
macro (a user-defined function, or UDF) you can immediately
use it as an Excel function. If it is a Sub (subroutine)-
type macro you can run it from the Excel Tools > Macro
menu (Alt-TMM).


And incidentally, I set this up so that you will get an error if one of the cells contains more than one byte.
 
Upvote 0
Wow! Thanks for that perfect help...but I still have a problem with this code:

it says (translated from german) ~

"Error compiling:
Sub or Function not defined"

and the "hex2dec" in your code is highlighted.

I followed your instructions but there seems to be something else missing....
 
Upvote 0
Hi,

I am trying to use this vba with excel 2010.

I have added the module and checked that the Analysis Toolpak add-ins are installed (Anaysis Toolpak and Analysis Toolpak-VBA)

Also within VB:
Tools > References > atpvbaen.xls. Confirmed it is there and selected.

Unfortunately I am getting a compile error. Sub or Function not defined.

Then (Function ChkSumXOR(Rin As Range) As String) is highlighted in yellow and
Dec2Hex is highlighted in Blue.


Can someone please help with this issue?


Thanks
Daniel.
 
Upvote 0
Just a bump...

Can anyone take a guess at what is goining on?

It's driving me nuts. Google is not being my friend with this one.



Regards
Daniel.
 
Upvote 0
Those functions are now just built in to Excel. So you can access them by means of the WorksheetFunction object, no need for anyof the Analysis Tookpak nonsense.

Code:
Function ChkSumXOR_2010(Rin As Range) As String
    '// originally written by Damon Ostrander
    '// edited by Greg Truby to work in Excel 2010
    '// see http://www.mrexcel.com/forum/showthread.php?t=46317
    '// for more information
    Dim Cell    As Range, _
        wf      As Excel.WorksheetFunction
    Set wf = Application.WorksheetFunction
    Let ChkSumXOR_2010 = 0
 
    For Each Cell In Rin
        If Not IsEmpty(Cell) Then
            If Len(Cell) < 3 Then
                ChkSumXOR_2010 = wf.Dec2Hex(wf.Hex2Dec(ChkSumXOR_2010) Xor wf.Hex2Dec(Cell))
            Else
                ChkSumXOR_2010 = 0
                MsgBox "Error, cell " & Cell.Address & " contains multi-byte value", _
                vbError, "Checksum Error"
            End If
        End If
    Next Cell
 
    If Len(ChkSumXOR_2010) = 1 Then ChkSumXOR_2010 = "0" & ChkSumXOR_2010
End Function

_________________
edited per feedback provided by Reload2000 in post 11
 
Last edited:
Upvote 0
Hi Guys, I'm another newbie with exactly the same requirement. I have tried both the above and am getting the same error as sydinstaller when I try Damon's method and when I use Greg's revised method all I get is the bottom number in the column of hex values.

I thought I had the perfect solution to my problem but am obviously missing something. I have never used VBE before so if someone could explain what to do I'd really appreciate it.

Thanks in advance

Ed
 
Upvote 0

Forum statistics

Threads
1,215,509
Messages
6,125,215
Members
449,215
Latest member
texmansru47

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top