2016-02-29 17 views
2

我剛剛在我的部門發佈了一個Excel加載項,我一直在爲最近2個月的時間內檢查大約30個驗證錯誤。我在所有情況下都處理錯誤陷阱(因爲它現在正在出現),但是今天我收到了一個可怕的喚醒電話,因爲我收到了兩個重要錯誤的自動電子郵件(我構建在錯誤處理中的一個功能)。我已經發布了關於第一個錯誤here的問題,並認爲我會爲第二個錯誤開始一個新問題,因爲它與第一個錯誤無關。#REF!粘貼爲值,但​​循環時沒有看到這樣

我的代碼如下

Private Sub symbolCheck() 
On Error GoTo ErrHandler 
Application.StatusBar = "(3/16) Checking for invalid symbols" 

Dim MyArray As Variant 
Dim replacementsMade As Boolean 
replacementsMade = False 
MyArray = ActiveSheet.UsedRange 

For i = LBound(MyArray) To UBound(MyArray) 
    For j = LBound(MyArray, 2) To UBound(MyArray, 2) 
     If MyArray(i, j) <> "" Then 
      'Apostrophe/Closing Single Quote 
      If InStr(1, MyArray(i, j), "’") > 0 Then 
       MyArray(i, j) = WorksheetFunction.Substitute(MyArray(i, j), "’", Chr(39)) 
       If replacementsMade = False Then 
        replacementsMade = True 
       End If 
      End If 
      'Apostrophe 
      If InStr(1, MyArray(i, j), "`") > 0 Then 
       MyArray(i, j) = WorksheetFunction.Substitute(MyArray(i, j), "`", Chr(39)) 
       If replacementsMade = False Then 
        replacementsMade = True 
       End If 
      End If 
      'Opening Single Quote 
      If InStr(1, MyArray(i, j), "‘") > 0 Then 
       MyArray(i, j) = WorksheetFunction.Substitute(MyArray(i, j), "‘", Chr(39)) 
       If replacementsMade = False Then 
        replacementsMade = True 
       End If 
      End If 
      'Double Open Quotes 
      If InStr(1, MyArray(i, j), "「") > 0 Then 
       MyArray(i, j) = WorksheetFunction.Substitute(MyArray(i, j), "「", """") 
       If replacementsMade = False Then 
        replacementsMade = True 
       End If 
      End If 
      'Double Closing Quotes 
      If InStr(1, MyArray(i, j), "」") > 0 Then 
       MyArray(i, j) = WorksheetFunction.Substitute(MyArray(i, j), "」", """") 
       If replacementsMade = False Then 
        replacementsMade = True 
       End If 
      End If 
      'Dash 
      If InStr(1, MyArray(i, j), "–") > 0 Then 
       MyArray(i, j) = WorksheetFunction.Substitute(MyArray(i, j), "–", "-") 
       If replacementsMade = False Then 
        replacementsMade = True 
       End If 
      End If 
      'Registered Trademark (R) 
      If InStr(1, MyArray(i, j), "®") > 0 Then 
       MyArray(i, j) = WorksheetFunction.Substitute(MyArray(i, j), "®", "(R)") 
       If replacementsMade = False Then 
        replacementsMade = True 
       End If 
      End If 
      'Trademark (TM) 
      If InStr(1, MyArray(i, j), "™") > 0 Then 
       MyArray(i, j) = WorksheetFunction.Substitute(MyArray(i, j), "™", "(TM)") 
       If replacementsMade = False Then 
        replacementsMade = True 
       End If 
      End If 
      'Degree Symbol 
      If InStr(1, MyArray(i, j), "°") > 0 Then 
       MyArray(i, j) = WorksheetFunction.Substitute(MyArray(i, j), "°", " degrees") 
       If replacementsMade = False Then 
        replacementsMade = True 
       End If 
      End If 
      'Multiplication/x Symbol 
      If InStr(1, MyArray(i, j), "×") > 0 Then 
       MyArray(i, j) = WorksheetFunction.Substitute(MyArray(i, j), "×", "x") 
       If replacementsMade = False Then 
        replacementsMade = True 
       End If 
      End If 
      'Upside-Down Question Mark Symbol 
      If InStr(1, MyArray(i, j), "¿") > 0 Then 
       MyArray(i, j) = WorksheetFunction.Substitute(MyArray(i, j), "¿", "") 
       If replacementsMade = False Then 
        replacementsMade = True 
       End If 
      End If 
      'Solid Bullet Symbol 
      If InStr(1, MyArray(i, j), "•") > 0 Then 
       MyArray(i, j) = WorksheetFunction.Substitute(MyArray(i, j), "•", "") 
       If replacementsMade = False Then 
        replacementsMade = True 
       End If 
      End If 
      'Triple Dots Symbol 
      If InStr(1, MyArray(i, j), "…") > 0 Then 
       MyArray(i, j) = WorksheetFunction.Substitute(MyArray(i, j), "…", "...") 
       If replacementsMade = False Then 
        replacementsMade = True 
       End If 
      End If 
      'Euro Symbol 
      If InStr(1, MyArray(i, j), "€") > 0 Then 
       MyArray(i, j) = WorksheetFunction.Substitute(MyArray(i, j), "€", "") 
       If replacementsMade = False Then 
        replacementsMade = True 
       End If 
      End If 
      'Linebreak Symbol 
      If InStr(1, MyArray(i, j), "|") > 0 Then 
       MyArray(i, j) = WorksheetFunction.Substitute(MyArray(i, j), "|", ",") 
       If replacementsMade = False Then 
        replacementsMade = True 
       End If 
      End If 
'   'Less Than Symbol 
'   If InStr(1, MyArray(i, j), "<") > 0 Then 
'    MyArray(i, j) = WorksheetFunction.Substitute(MyArray(i, j), "<", "<") 
'   End If 
'   'Greater Than Symbol 
'   If InStr(1, MyArray(i, j), ">") > 0 Then 
'    MyArray(i, j) = WorksheetFunction.Substitute(MyArray(i, j), ">", ">") 
'   End If 
      'Half Fraction 
      If InStr(1, MyArray(i, j), "½") > 0 Then 
       MyArray(i, j) = WorksheetFunction.Substitute(MyArray(i, j), "½", " 1/2") 
       If replacementsMade = False Then 
        replacementsMade = True 
       End If 
      End If 
      'Three Quarter Fraction 
      If InStr(1, MyArray(i, j), "¾") > 0 Then 
       MyArray(i, j) = WorksheetFunction.Substitute(MyArray(i, j), "¾", " 3/4") 
       If replacementsMade = False Then 
        replacementsMade = True 
       End If 
      End If 
      'One Quarter Fraction 
      If InStr(1, MyArray(i, j), "¼") > 0 Then 
       MyArray(i, j) = WorksheetFunction.Substitute(MyArray(i, j), "¼", " 1/4") 
       If replacementsMade = False Then 
        replacementsMade = True 
       End If 
      End If 
     End If 
    Next j 
Next i 

If replacementsMade Then 
    ActiveSheet.UsedRange = MyArray 
End If 

Set MyArray = Nothing 

Exit Sub 
ErrHandler: 
    Err.Raise Err.Number, "symbolCheck", Err.Description 
End Sub 

上線發生這種錯誤

If MyArray(i, j) <> "" Then 

= 209和Ĵ = 60,所以我做了一些戳看去在數組中查看該位置的值。當我查看數組插槽的監視列表值時,該值僅表示Error 2023。所以,我看着與那些j對應的單元格的值和唉,我終於明白了爲什麼錯誤被提出。單元格中的值最初是一個帶有引用錯誤的公式,並且因爲我在運行此子文件之前將其複製/粘貼爲值,所以我認爲我會很好。我不知道#REF!沒有被視爲明文?

這使我對我的問題

我如何處理這種情況?更確切地說,如果#REF!即使在作爲值的複製/粘貼之後仍未被視爲明文,我如何能夠在電子表格中刪除#REF!值(不使用「查找/替換」)?

+0

''#REF <> 「」,所以它出現在你的if語句是工作的罰款? – findwindow

+0

不,該行是當被檢查的值是'#REF'時發生錯誤的地方。它應該通過if語句中的代碼,如果它被視爲文本,就像其他所有內容一樣。 – CaffeinatedCoder

回答

2

解決方案清除#REF!電子表格中的值

您可以使用SpecialCells清除錯誤。

ActiveSheet.UsedRange.SpecialCells(xlCellTypeFormulas, xlErrors).ClearContents 'Or change .Value to another value, delete cells, etc. as desired 

處理#REF的解決方案!陣列中的錯誤

您可以使用ISERROR()VBA函數來捕獲每個#REF!然後根據需要進行處理。

修改代碼如下:

Private Sub symbolCheck() 
On Error GoTo ErrHandler 
Application.StatusBar = "(3/16) Checking for invalid symbols" 

Dim MyArray As Variant 
Dim replacementsMade As Boolean 
replacementsMade = False 
MyArray = ActiveSheet.UsedRange 

For i = LBound(MyArray) To UBound(MyArray) 
    For j = LBound(MyArray, 2) To UBound(MyArray, 2) 
     If IsError(MyArray(i, j)) Then 
      'Handle the #REF! here 

     ElseIf MyArray(i, j) <> "" Then 
      'Apostrophe/Closing Single Quote 
      If InStr(1, MyArray(i, j), "’") > 0 Then 
       MyArray(i, j) = WorksheetFunction.Substitute(MyArray(i, j), "’", Chr(39)) 
       If replacementsMade = False Then 
        replacementsMade = True 
       End If 
      End If 
      'Apostrophe 
      If InStr(1, MyArray(i, j), "`") > 0 Then 
       MyArray(i, j) = WorksheetFunction.Substitute(MyArray(i, j), "`", Chr(39)) 
       If replacementsMade = False Then 
        replacementsMade = True 
       End If 
      End If 
      'Opening Single Quote 
      If InStr(1, MyArray(i, j), "‘") > 0 Then 
       MyArray(i, j) = WorksheetFunction.Substitute(MyArray(i, j), "‘", Chr(39)) 
       If replacementsMade = False Then 
        replacementsMade = True 
       End If 
      End If 
      'Double Open Quotes 
      If InStr(1, MyArray(i, j), "「") > 0 Then 
       MyArray(i, j) = WorksheetFunction.Substitute(MyArray(i, j), "「", """") 
       If replacementsMade = False Then 
        replacementsMade = True 
       End If 
      End If 
      'Double Closing Quotes 
      If InStr(1, MyArray(i, j), "」") > 0 Then 
       MyArray(i, j) = WorksheetFunction.Substitute(MyArray(i, j), "」", """") 
       If replacementsMade = False Then 
        replacementsMade = True 
       End If 
      End If 
      'Dash 
      If InStr(1, MyArray(i, j), "–") > 0 Then 
       MyArray(i, j) = WorksheetFunction.Substitute(MyArray(i, j), "–", "-") 
       If replacementsMade = False Then 
        replacementsMade = True 
       End If 
      End If 
      'Registered Trademark (R) 
      If InStr(1, MyArray(i, j), "®") > 0 Then 
       MyArray(i, j) = WorksheetFunction.Substitute(MyArray(i, j), "®", "(R)") 
       If replacementsMade = False Then 
        replacementsMade = True 
       End If 
      End If 
      'Trademark (TM) 
      If InStr(1, MyArray(i, j), "™") > 0 Then 
       MyArray(i, j) = WorksheetFunction.Substitute(MyArray(i, j), "™", "(TM)") 
       If replacementsMade = False Then 
        replacementsMade = True 
       End If 
      End If 
      'Degree Symbol 
      If InStr(1, MyArray(i, j), "°") > 0 Then 
       MyArray(i, j) = WorksheetFunction.Substitute(MyArray(i, j), "°", " degrees") 
       If replacementsMade = False Then 
        replacementsMade = True 
       End If 
      End If 
      'Multiplication/x Symbol 
      If InStr(1, MyArray(i, j), "×") > 0 Then 
       MyArray(i, j) = WorksheetFunction.Substitute(MyArray(i, j), "×", "x") 
       If replacementsMade = False Then 
        replacementsMade = True 
       End If 
      End If 
      'Upside-Down Question Mark Symbol 
      If InStr(1, MyArray(i, j), "¿") > 0 Then 
       MyArray(i, j) = WorksheetFunction.Substitute(MyArray(i, j), "¿", "") 
       If replacementsMade = False Then 
        replacementsMade = True 
       End If 
      End If 
      'Solid Bullet Symbol 
      If InStr(1, MyArray(i, j), "•") > 0 Then 
       MyArray(i, j) = WorksheetFunction.Substitute(MyArray(i, j), "•", "") 
       If replacementsMade = False Then 
        replacementsMade = True 
       End If 
      End If 
      'Triple Dots Symbol 
      If InStr(1, MyArray(i, j), "…") > 0 Then 
       MyArray(i, j) = WorksheetFunction.Substitute(MyArray(i, j), "…", "...") 
       If replacementsMade = False Then 
        replacementsMade = True 
       End If 
      End If 
      'Euro Symbol 
      If InStr(1, MyArray(i, j), "€") > 0 Then 
       MyArray(i, j) = WorksheetFunction.Substitute(MyArray(i, j), "€", "") 
       If replacementsMade = False Then 
        replacementsMade = True 
       End If 
      End If 
      'Linebreak Symbol 
      If InStr(1, MyArray(i, j), "|") > 0 Then 
       MyArray(i, j) = WorksheetFunction.Substitute(MyArray(i, j), "|", ",") 
       If replacementsMade = False Then 
        replacementsMade = True 
       End If 
      End If 
'   'Less Than Symbol 
'   If InStr(1, MyArray(i, j), "<") > 0 Then 
'    MyArray(i, j) = WorksheetFunction.Substitute(MyArray(i, j), "<", "<") 
'   End If 
'   'Greater Than Symbol 
'   If InStr(1, MyArray(i, j), ">") > 0 Then 
'    MyArray(i, j) = WorksheetFunction.Substitute(MyArray(i, j), ">", ">") 
'   End If 
      'Half Fraction 
      If InStr(1, MyArray(i, j), "½") > 0 Then 
       MyArray(i, j) = WorksheetFunction.Substitute(MyArray(i, j), "½", " 1/2") 
       If replacementsMade = False Then 
        replacementsMade = True 
       End If 
      End If 
      'Three Quarter Fraction 
      If InStr(1, MyArray(i, j), "¾") > 0 Then 
       MyArray(i, j) = WorksheetFunction.Substitute(MyArray(i, j), "¾", " 3/4") 
       If replacementsMade = False Then 
        replacementsMade = True 
       End If 
      End If 
      'One Quarter Fraction 
      If InStr(1, MyArray(i, j), "¼") > 0 Then 
       MyArray(i, j) = WorksheetFunction.Substitute(MyArray(i, j), "¼", " 1/4") 
       If replacementsMade = False Then 
        replacementsMade = True 
       End If 
      End If 
     End If 
    Next j 
Next i 

If replacementsMade Then 
    ActiveSheet.UsedRange = MyArray 
End If 

Set MyArray = Nothing 

Exit Sub 
ErrHandler: 
    Err.Raise Err.Number, "symbolCheck", Err.Description 
End Sub 
+0

這是完美的!我很高興至少有一些簡單的方法來處理這個問題。謝謝!儘管'#REF!'不被視爲純文本,我仍然感到困惑。我甚至在單元格中放了一個普通的'#',看看Excel的編程是否仍然以不同的方式查看以'#'開頭的語句(因爲在除此之外的情況下它們通常是不同的和顯着的)。等待10分鐘後,我會接受你的答案。 – CaffeinatedCoder

+0

太棒了!我很高興它適合你:) –