# XOR and other things

#### TinaP

##### Well-known Member
I recently acquainted myself with the XOR operator in VBA and have found it very useful over the past couple days, which got me wondering...

1) Does anyone else use the XOR operator? and,
B) Are there any operators/statements/functions that you've learned belatedly?

Disclaimer: I realize I could have used the OR function, but in the project I was working on, I would have needed two more lines of code and nested IF statements; so don't judge me.

### Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

#### xenou

##### MrExcel MVP
I believe I implemented XOR in a solution once but forget what that was (I might still be using it for all I know - I have a lot of code running now).

Other items I discovered belatedly:
Name ... As ... --> rename a file
Dim var As String * 20 --> declared a fixed width string

Last edited:

#### ZVI

##### MrExcel MVP
If A and B are numbers then (A Xor B) Xor B = A
Assuming B is ASC code of the (next) char in the password, the Xor can be used in password encryption / decryption.
Like this:
Rich (BB code):
``````Sub XorUsage()

Dim Pwd As String

' Put into A1 the text to be encrypted /decrypted
[A2:A4].ClearContents
If Len([A1].Value) = 0 Then [A1].Value = "The quick brown fox jumps over the lazy dog"

If Len(Pwd) = 0 Then Exit Sub

' Put encrtypded A1 to A2
[A2].Value = StrXor([A1].Value, Pwd)

' Put Decrypted A2 to A3
[A3].Value = StrXor([A2].Value, Pwd)

' Compare Decrypted A3 with Original A1
[A4].Formula = "=A1=A3"

End Sub

Function StrXor(Txt As String, Pwd As String) As String
Dim a As Integer, b As Integer, c As Integer, i As Long, j As Long
For i = 1 To Len(Txt)
j = j + 1
If j > Len(Pwd) Then j = 1
a = Asc(Mid(Txt, i))
b = Asc(Mid(Pwd, j))
c = a Xor b   ' <-- Encription/Decription
c = c Xor 255 ' <-- This excludes Chr(0)
StrXor = StrXor & Chr(c)
Next
End Function``````

Last edited:

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

1,164,659
Messages
5,838,643
Members
430,558
Latest member
Krampus

### 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.

### Which adblocker are you using?

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

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