views:

75

answers:

3

I think the title says it all. Eventually, I want to move the cell to the location where the last error occured. Edit: Forgot to say that I'm using Excel 2003.

A: 

Could this be done with an error handler?

An example of what I mean below:

sub code1()
on error goto cell A1
end sub
Waller
The error that I meant might be also caused by my custom error condition (using IF). So, the problem is how to get the last used cell by VBA code when the last "mistake/error" occured in a Workbook/Worksheet? AFAIK, the ActiveCell object refers to current cell in the window, not to the current cell processed by VBA code.
Vantomex
+1  A: 

Wrap your VBA function in another function that stores the cell location and value as variants. Keep this 'wrapper' function as basic as possible so it won't cause any additional errors.

If you're trying to debug app-crashing errors, the wrapper function could even store those values in a comma-delimited text file. Once stored, Excel can crash all it wants and you'll still know what the cell location and value were since you stored them outside of Excel beforehand.

PowerUser
+1  A: 

As requested in comments...

Look up the 'Caller' property of the 'Application' object in the Excel VBA help. When you use it from a VBA routine, it will tell you where the call to the routine came from - what Range, Chart, etc.

An important thing to be aware of when using 'Application.Caller' is that it isn't always a Range object. Look at the help, but the property returns a Variant value that can be a Range, String, or Error. (It is a Range object in the case you're interested in, but you'll need to be aware of this.)

Because of the above, and the vagaries of VBA syntax when it comes to objects vs. values, it can be tricky to use 'Application.Caller'. Putting a line like:

Debug.Print Application.Caller.Address

in your code will fail when the caller isn't a Range. Doing something like:

Dim v
v = Application.Caller

will "compile", but will create circular references when the caller is a Range because you're trying to access the value of the calling Range.

This all means that it's probably best to write a little utility function for yourself:

Public Function currentCaller() As String
    If TypeOf Application.Caller Is Range Then
        Dim rng As Range
        Set rng = Application.Caller

        currentCaller = rng.Address(External:=True)
    Else
        currentCaller = CStr(Application.Caller)
    End If
End Function

and then call it from your error handlers where you want to know where the call came from.

One more thing - obviously this can only tell you the caller once a VBA routine has actually been called. If you have errors in your calling formulas, Excel will return error values to your cells without ever calling your VBA routines.

jtolle