2016-07-18 57 views
0

我正在通過添加字符的ASCII來處理數據屏蔽工具,但是一旦達到最大ASCII數字,它就會發生錯誤。通過更改字符屏蔽Excel宏數據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

你可以包括這樣的一段代碼: -

 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 
+0

您好,感謝的一段代碼。然而,當運行一串字符的代碼時會出現新的故障。它會在每次運行宏時複製單元格值。 例子 Å>÷÷÷÷÷÷÷÷ – Silverchain

+0

這很奇怪,我看不出它是如何添加比原始字符串更多的字符的。我將把我的整個代碼放在一個調試語句的答案中去嘗試。 –

+0

它現在完美運作。謝謝! – Silverchain