Run to Cursor

Many times when you're executing code, you don't want to run every line of code line by line, but executing the entire procedure at once doesn't help you debug the procedure. If you have a long loop within the procedure, it can be very tedious to execute every line of the loop every time the loop needs to run. For example, consider the following code:

Sub CoffeeTime()

Dim curLatteAllowance As Currency Dim curLattePrice As Currency Dim intNumLattes As Integer Dim curTotalExpenses As Currency curLattePrice = 3.5

curLatteAllowance = InputBox("Enter the amount of money you have for lattes." _ , "Latte Allowance")

While curTotalExpenses < curLatteAllowance intNumLattes = intNumLattes + 1

curTotalExpenses = curTotalExpenses + curLattePrice

Wend

Debug.Print intNumLattes

MsgBox "You can purchase" & intNumLattes & "lattes.", _ vbOkOnly, "Total Lattes" End Sub

If you have $350 to spend on lattes, the While...Wend loop will run 100 times. Pressing F8 to step through that long of a loop can be very tedious. If you're not worried that the loop is producing incorrect data, you can place your cursor in the Debug.Print intNumLattes line and press Ctrl+F8. Your procedure will run until it reaches the Debug.Print line, then it halts. The line is highlighted. You can then press F8 to execute the highlighted line of code or press F5 to continue execution until the end of the procedure.

0 0

Responses

  • mary
    How to "Run to cursor" Access VBA?
    7 years ago

Post a comment