I'm working on a data masking tools by adding the ASCII of the characters, however it will hit an error once it reached the maximum ASCII number.
Is there any way to prevent or ignore the adding of the ASCII number if it's already reaching the maximum ASCII number?
My excel Code
Sub DataMask()
Dim rngCell As Range
Dim intChar As Integer
Dim strCheckString As String
Dim strCheckChar As String
Dim intCheckChar As Integer
Dim strClean As String
For Each rngCell In Selection
strCheckString = rngCell.Value
strClean = ""
intChar = 1
If strCheckString <> "" Then
For intChar = 1 To Len(strCheckString)
strCheckChar = Mid(strCheckString, intChar, 1)
intCheckChar = Asc(strCheckChar) + 68
strClean = strClean & Chr(intCheckChar)
Next intChar
rngCell.Value = strClean
End If
Next rngCell
End Sub
You could include a piece of code like this:-
If intCheckChar <= 255 Then
newCheckChar = Chr(intCheckChar)
Else
newCheckChar = strCheckChar
End If
strClean = strClean & newCheckChar
to replace
strClean = strClean & Chr(intCheckChar)
so if the new character has a code more than 255 it just uses the old character.
This is the whole sub with a couple of debug statements:-
sub DataMask()
Dim rngCell As Range
Dim intChar As Integer
Dim strCheckString As String
Dim strCheckChar As String
Dim intCheckChar As Integer
Dim strClean As String
Dim newCheckChar As String
For Each rngCell In Selection
strCheckString = rngCell.Value
strClean = ""
intChar = 1
If strCheckString <> "" Then
For intChar = 1 To Len(strCheckString)
strCheckChar = Mid(strCheckString, intChar, 1)
intCheckChar = Asc(strCheckChar) + 68
' New code
If intCheckChar <= 255 Then
newCheckChar = Chr(intCheckChar)
Else
newCheckChar = strCheckChar
End If
strClean = strClean & newCheckChar
Debug.Print ("intChar=" & intChar)
Debug.Print ("intCheckChar=" & intCheckChar)
' end of code
Next intChar
rngCell.Value = strClean
End If
Next rngCell
End Sub