Finally, I came up with this structure, for looping only non empty cells :
Sub TestWrong()
Dim cell As Range, chk As Long, i As Long, str1 As String
For Each cell In Range("A2:A1000")
If Not IsEmpty(cell) Then
str1 = cell.Value
chk = Asc(Left$(str1, 1))
For i = 2 To Len(str1)
chk = chk Xor Asc(Mid$(str1, i, 1))
Next i
cell.Offset(, 2).Value = cell.Value & Right$("00" & Hex$(chk), 2)
End If
Next cell
End Sub
Now I understand better why we need :
chk = Asc(Left$(str1, 1))
For i = 2 To Len(str1)
I have checked the hex xoring result with this function and it works fine :
Function Chkxor(ByVal X As String) As String
Dim S As Byte, P As Integer
For P = 1 To Len(X)
S = S Xor Asc(Mid$(X, P, 1))
Next P
Chkxor = Right$("0" & Hex$(S), 2)
End Function
Because I do not want to limit the numbers of strings to a given value (can be unknown and not preset Z strings), I have tried to express the working range by using :
For Each cell In Range("A2:A")
If Not IsEmpty(cell) Then
'do sth
End If
Next
But the code won't work, it returns an error saying range not defined, or smtg like that. Any idea ?
Here is the file for you to check. Again, thank you very much Karedog for your patient explanations.
Bookmarks