#REF! 粘贴作为价值,但循环时没有看到

我刚刚在我的部门发布了一个Excel加载项,我已经在最近2个月的时间里检查了大约30个validation错误。 我在所有情况下都处理错误陷阱(因为它现在正在出现),但是今天我收到了一个可怕的唤醒电话,因为我收到了两个重要的错误的自动电子邮件(这是我在error handling中构build的一个function)。 我已经在这里发布了关于第一个bug的问题,并且认为我会为第二个bug提出一个新的问题,因为它与第一个bug无关。

我的代码如下

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和j = 60,所以我做了一些探头,看看arrays内部,看看在那个位置有什么价值。 当我查看数组插槽的监视列表值时,该值仅表示Error 2023 。 所以,我看着与那些j值相对应的单元格,唉,我终于明白了为什么会出现错误。 单元格中的值最初是一个带有引用错误的公式,因为我在复制/粘贴之前运行此子的值,我认为我会没事的。 我不知道#REF! 没有被视为明文?

这导致我的问题

我该如何处理这种情况? 更确切地说,我将如何摆脱#REF! 电子表格中的值(不使用“查找/replace”) #REF! 即使作为一个值复制/粘贴后,不被视为明文?

解决scheme清除#REF! 电子表格中的值

您可以使用SpecialCells清除错误。

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

处理#REF的解决scheme! 数组中的错误

您可以使用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