#REF!粘贴为值,但​​在循环

时间:2016-02-29 23:42:34

标签: excel vba excel-vba runtime-error

我刚刚在我的部门发布了一个Excel加载项,我在过去的2个多月里一直在检查大约30个验证错误。我在所有情况下都处理了错误陷阱(现在就出现了),但是今天我接到一个可怕的叫醒电话,因为我收到了两个重要错误的自动电子邮件(我在错误处理中构建的一个功能)。我已经发布了关于第一个错误here的问题,并且认为我会为第二个错误开始一个新问题,因为它与第一个错误无关。

我的代码如下

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

i = 209且 j = 60时,我做了一些探讨并查看数组内部以查看该位置的值。当我查看数组插槽的Watchlist值时,该值只显示Error 2023。所以,我查看了与那些 i j 值相对应的单元格,唉,我终于看到了为什么会出现错误。单元格中的值最初是一个带引用错误的公式,因为我在运行此子程序之前将其复制/粘贴为值,我认为我没事。我不知道#REF!没有被视为明文?

这引出了我的问题

我该如何处理这种情况?更准确地说,如果#REF!即使在作为值复制/粘贴后被视为纯文本,我将如何摆脱电子表格中的#REF!值(不使用查找/替换) ?

1 个答案:

答案 0 :(得分: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