View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Jacob Skaria Jacob Skaria is offline
external usenet poster
 
Posts: 8,520
Default Identify offending cell

Since the variables are declared as Integer it expects a integer value. If
one of the cell contains a text value it would give a typemismatch. I have
done a small change to your code as below which will retrieve 0 for a text
value...

Sub ErrorExample1()

On Error Resume Next

Dim total As Integer
Dim intA As Integer
Dim intB As Integer
Dim intC As Integer

intA = Val(Range("A1"))
intB = Val(Range("A2"))
intC = Val(Range("A3"))

total = intA + intB + intC

MsgBox ("Total is " & total)

End Sub

Also try the below example which will give you the sum and at the same time
tell you which cell is having a non-numeric value. Hope this helps

Sub ErrorExample2()

Dim rngTemp As Range
Set rngTemp = Range("A1:A3")
For Each cell In rngTemp
If Not IsNumeric(cell.Value) Then MsgBox _
cell.Address & " is having a non-numeric value"
Next
MsgBox "Sum of " & rngTemp.Address & " is " & _
WorksheetFunction.Sum(rngTemp)

End Sub

If this post helps click Yes
---------------
Jacob Skaria


"linglc" wrote:

I have these codes below. What I want to do is to identify in a message box
which cell has the error. So if I have text in cell A3, I want the message
box to tell me the error is in A3. Any ideas?

Sub ErrorExample()

On Error Resume Next

Dim total As Integer
Dim intA As Integer
Dim intB As Integer
Dim intC As Integer

intA = Range("A1")
intB = Range("A2")
intC = Range("A3")

total = intA + intB + intC

MsgBox ("Total is " & total)

End Sub