如果#REF! 然后find错误框macros
我正在尝试完成一个简单的macros,寻找“#REF!” 在工作表中由于用户指定一行并破坏基础公式。
我已经find了:
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
从我的理解,我需要input一个If参数是真的
MsgBox"Please go back and check...."
我只是不知道该怎么做,如果…
任何指针将非常感激。
尝试下面的代码
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
使用这个,将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
预计可能会有多个单元格出现这样的错误,您可能必须在Do...While
循环中实现您的.Find
。 SO上有这样几个问题的例子。 如果您在执行循环时遇到困难,请告诉我。
你有问题是因为你正在寻找一个string – 而#REF
是一个错误。
您可以使用IsError函数,为具有错误的单元格返回true。 结合这个循环,你可以实现你所需要的。 我没有testing过这个,但是你得到了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