문제

I am trying to complete a simple macro that looks for '#REF!' in a worksheet due to a user alterting a row and ruining underlying formulas.

I have got as far as the 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

From what I understand I need to enter an If parameter is true then

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

Im just not sure what should follow the if....

Any pointers would be very much appreciated.

도움이 되었습니까?

해결책

Try below code

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

다른 팁

Use this, change the LookIn argument to xlValues instead of xlFormulas:

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

For a bit cleaner implementation:

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

Anticipating there may be multiple cells with an error like this, you'll probably have to implement your .Find within a Do...While loop. There are several examples of that sort of problem on SO. If you have trouble implementing the loop, let me know.

The problem you have is because you're searching for a string - whereas #REF is an error.

You could use the IsError function, to return true for a cell with an error. Combine this with a loop, and you could achieve what you require. I haven't tested this, but you get the 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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top