2

我正在尝试完成一个查找“#REF!”的简单宏 由于用户更改了一行并破坏了基础公式,因此在工作表中。

我已经找到了:

Sheets("Location_Multiple").Select
Range("A1:AL10000").Select

Selection.Find(What:="#REF!", After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False).Activate

据我了解,我需要输入一个 If 参数是 true 然后

MsgBox"Please go back and check...."

我只是不确定如果……应该遵循什么……

任何指针将不胜感激。

4

3 回答 3

5

试试下面的代码

Sub DisplayError()

On Error Resume Next
Dim rng As Range
Set rng = Sheets("Location_Multiple").Range("A1:AL10000")

Dim rngError As Range
Set rngError = rng.SpecialCells(xlCellTypeFormulas, xlErrors)

If Not rngError Is Nothing Then
    For Each cell In rngError
        MsgBox "Please go back and check.... " & cell.Address
    Next
End If
End Sub
于 2013-09-26T09:49:45.857 回答
1

使用它,将LookIn参数更改为,xlValues而不是xlFormulas

Selection.Find(What:="#REF!", After:=ActiveCell, LookIn:=xlValues, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False).Activate

对于更清洁的实现:

Dim sht as Worksheet
Dim rngSrch as Range
Dim rngErr as Range

Set sht = Sheets("Location_Multiple")
Set rngSrch = sht.Range("A1:AL10000")

Set rngErr = rngSearch.Find(What:="#REF!", After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False)

If Not rngErr Is Nothing Then
    'Do something to the offending cell, like, highlight it:
    rngErr.Interior.ColorIndex = 39
End If

预计可能有多个单元格出现这样的错误,您可能必须.FindDo...While循环中实现您的。SO上有几个这类问题的例子。如果您在实现循环时遇到问题,请告诉我。

于 2013-09-26T14:14:00.393 回答
0

您遇到的问题是因为您正在搜索一个字符串 - 而#REF这是一个错误。

您可以使用IsError函数为有错误的单元格返回 true。将其与循环结合起来,您就可以实现您所需要的。我没有测试过这个,但你得到了 jist:

Set rng = Sheets("Location_Multiple").Range("A1:AL10000")
For Each cell In rngError
    If IsError(cell) == true
    MsgBox "Please go back and check.... " & cell.Address
    Endif
Next
于 2013-09-26T10:01:21.517 回答