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

时间:2016-07-18 02:47:47

标签: excel macros data-masking

我通过添加字符的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

1 个答案:

答案 0 :(得分:1)

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

       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