Example A Simple Program to Trace

Sub Test()

Dim ws As Worksheet Set ws = ActiveSheet

' Insert a value into cell A1 ws.Cells(1, 1).Value = "sample"

' Paste value only ws.Cells(2, 1).PasteSpecial Paste:=xlValues End Sub

Make sure that an empty worksheet is active in Excel. Switch to the VBA IDE and place the insertion point somewhere in the code. Then hit the F8 key once, which starts the tracing process. (You can also choose Step Into from the Debug menu.)

Continue striking the F8 key, pausing between keystrokes to view the effect of each instruction in the Excel window. (You can toggle between Excel and the IDE using Alt-Fll.) As you trace through this code, you will see the word "sample" entered into cell A1 of the active worksheet, changed to appear in boldface, copied to the Clipboard, and pasted as normal text into the cell A2. Then you can begin to see what Excel VBA programming is all about!

Let us discuss some of the tools that Excel provides for debugging code.

0 0

Responses

Post a comment