I am trying to add code that will search for the string #DIV/0! in a range of cells (EF:W10) and if found, will replace that cell with NA. Below is what I have, but it is not quite working. What am I missing?
Dim rngDiv As Range
For Each rngDiv In Range("E4:W10")
If InStr(rngDiv.Value, "#DIV/0!") > 0 Then
rngDiv.Value = "NA"
End If
Next rngDiv
If you really need VBA to do this then you can use the IsError()
function
Dim rngDiv As Range
For Each rngDiv In Range("E4:W10")
If IsError(rngDiv) Then
rngDiv.Value = "NA"
End If
Next
However it's best to catch your errors in the actual formula rather than afterwards with VBA.
Note: The above will catch all errors - if you just want the #DIV/0!
error then you need to test for the .Text
property instead of the .Value
property:
Dim rngDiv As Range
For Each rngDiv In Range("E4:W10")
If rngDiv.Text = "#DIV/0!" Then
rngDiv.Value = "NA"
End If
Next rngDiv
How about:
Sub marine()
Dim rngDiv As Range
For Each rngDiv In Range("E4:W10")
If rngDiv.Text = "#DIV/0!" Then
rngDiv.Value = "NA"
End If
Next rngDiv
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.