Excel 通过改变字符 ASCII 来屏蔽宏数据

Excel macro data masking by changing character ASCII

我正在通过添加字符的 ASCII 来开发数据屏蔽工具,但是一旦达到最大 ASCII 码就会出错。

如果 ASCII 码已经达到最大 ASCII 码,是否有任何方法可以阻止或忽略添加 ASCII 码?

我的excel代码

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

您可以包含一段这样的代码:-

       If intCheckChar <= 255 Then
            newCheckChar = Chr(intCheckChar)
       Else
            newCheckChar = strCheckChar
       End If
       strClean = strClean & newCheckChar

替换

       strClean = strClean & Chr(intCheckChar)

因此,如果新字符的代码大于 255,它只会使用旧字符。

这是带有几个调试语句的整个子程序:-

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