catching "#DIV/0!" in a loop

S

sck10

Hello,

I am running the following to replace "alt+Enter". Whenever I run across a
cell with the value of "#DIV/0!" I get an error (Error = 2007). What I
don't understand is that the "#DIV/0!" is not a equation, but a value. Any
help with this would be appreciated.

Thanks, sck10


Range("rng_test").Select
Dim myCell As Range
'For Each myCell In Range("rng_template").Cells
For Each myCell In Selection.Cells
Range("rng_value").Value = myCell.Value

End If
If myCell.Value = "#DIV/0!" Then
myCell.Value = ""
End If

myCell.Value = Replace(myCell.Value, Chr(10), " ")
myCell.Value = Replace(myCell.Value, Chr(13), " ")
Next
 
B

Bernie Deitrick

Forget the loop, just use

Range("rng_test").Replace Chr(10), " "
Range("rng_test").Replace Chr(13), " "

HTH,
Bernie
MS Excel MVP
 
G

Guest

Not sure what you are specifically asking, but here is one approach.

Range("rng_test").Select
Dim myCell As Range
'For Each myCell In Range("rng_template").Cells
For Each myCell In Selection.Cells
Range("rng_value").Value = myCell.Value

End If
If myCell.Text = "#DIV/0!" Then
myCell.Value = ""
End If

myCell.Value = Replace(myCell.Value, Chr(10), " ")
myCell.Value = Replace(myCell.Value, Chr(13), " ")
Next


You can also do

If myCell.Value = cvErr(xlErrDiv0) Then
myCell.Value = ""
End If

or for any error
If iserror(myCell.Value) Then
myCell.Value = ""
End If

and a little demo from the immediate window:

? activecell.Text
#DIV/0!
? activecell.Value = cvErr(xlErrDiv0)
True
 
S

sck10

Thanks Tom,

Appreciate the code...

sck10


Tom Ogilvy said:
Not sure what you are specifically asking, but here is one approach.

Range("rng_test").Select
Dim myCell As Range
'For Each myCell In Range("rng_template").Cells
For Each myCell In Selection.Cells
Range("rng_value").Value = myCell.Value

End If
If myCell.Text = "#DIV/0!" Then
myCell.Value = ""
End If

myCell.Value = Replace(myCell.Value, Chr(10), " ")
myCell.Value = Replace(myCell.Value, Chr(13), " ")
Next


You can also do

If myCell.Value = cvErr(xlErrDiv0) Then
myCell.Value = ""
End If

or for any error
If iserror(myCell.Value) Then
myCell.Value = ""
End If

and a little demo from the immediate window:

? activecell.Text
#DIV/0!
? activecell.Value = cvErr(xlErrDiv0)
True
 
S

sck10

Thank you Bernie,

This helps greatly...

sck10


Bernie Deitrick said:
Forget the loop, just use

Range("rng_test").Replace Chr(10), " "
Range("rng_test").Replace Chr(13), " "

HTH,
Bernie
MS Excel MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top