ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Tracing problems with macros (https://www.excelbanter.com/excel-programming/355208-tracing-problems-macros.html)

No Name

Tracing problems with macros
 
Hi

Is there a trace function - for spotting where macros crash? I've been
trying to get one functioning all day and it doesn't help much (when as
green as I am) when all I get is an error!

Cheers.
Andy.



Mark

Tracing problems with macros
 
you can use the debugging information as a way of tracing where an
error occurrs. Set a breakpoint at the start of your macro by
leftclicking the Grey Margin to the left of the code line you want to
stop at. Once the code runs to there, the line will turn yellow. From
there you can press the "F8" button or select options from the Debug
toolbar. Eventually your error will occur and you'll know where.
Using the immediate window in tandem with this can be very handy. You
can type in questions like..."?Range("A1")" When you hit the enter
key, the value of A1 is displayed. Try the help files too, they are
very handy.


No Name

Tracing problems with macros
 
Brillinat! Thanks! That makes life easier. Is there any way of seeing the
spreadsheet as the macro runs - apart from arranging the Windows? When it
runs a For Next loop how do you know where it's up to?

Cheers.
Andy.

"Mark" wrote in message
ups.com...
you can use the debugging information as a way of tracing where an
error occurrs. Set a breakpoint at the start of your macro by
leftclicking the Grey Margin to the left of the code line you want to
stop at. Once the code runs to there, the line will turn yellow. From
there you can press the "F8" button or select options from the Debug
toolbar. Eventually your error will occur and you'll know where.
Using the immediate window in tandem with this can be very handy. You
can type in questions like..."?Range("A1")" When you hit the enter
key, the value of A1 is displayed. Try the help files too, they are
very handy.




Mark

Tracing problems with macros
 
You have to switch the views as far as I know. Another option, would
be to insert a debug.print statement into your loop. for example the
following will print the values of the first 10 cells in column A

Sub test()

Dim CurRng As Range
Dim Cnt As Integer

Application.ScreenUpdating = False
For Cnt = 0 To 9
Set CurRng = Range("A1").Offset(Cnt, 0)
Debug.Print "Range " & CurRng.Address & " = " & CurRng
Next Cnt
Application.ScreenUpdating = True

End Sub

I prefer this method as I often will turn off the screenupdating
process to smooth out performance.



All times are GMT +1. The time now is 02:22 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com