如何調試VBA及在Excel中調試的方法
下一篇: SWOT分析法

如何調試VBA及在Excel中調試的方法

分享成果,隨喜正能量】有一些人的出現,就是來給我們開眼的。所以,人一定要禁得起假話,受得住敷衍,忍得住欺騙,忘得了承諾,放得下一切,百鍊成精,淡定從容。。

VBA系列教程共十套,我已經全部完成,技術工具系列在不斷推出。從這些教程及工具中,大家可以看到VBA確實是一門實用技術。正如我給VBA的定義:VBA是個人小型自動化處理的有效工具。利用好了,可以大大提高自己的勞動效率,而且可以提高數據的準確度。

對於學員而言,如果您是入門階段可以打包選擇7.1.3.9教程,第7是入門,第1是入門後的提高,第3字典是必備的VBA之精華,第9是實用的典型案例講解。如果您有了VBA的一定基礎可以根據自己的需要,進行教程的選擇及工具的利用,教程提供的程序文件就如一座大型的代碼庫支持著大家的工作。

工具系列共分兩類:一類是容易些的YZ系列,一類是較難些的NZ系列。這些工具是大家學習VBA的好幫手。 對於急用的學員,可以立竿見影解決自己的工作效率問題。其中NZ系列工具還有專門版本資料,提供給已按部就班學習完我所有教程的學員。

VBA是面向對象編程,博大精深,包括我自己也在不斷的學習和提高,這裡我會分享些一些有用的片段,以彌補我教程的補足。這些片段為了大家更好的領會和掌握,我會用英語和漢語同時發布。今日的內容:如何調試VBA及在Excel中調試的方法

的利用,教程提供的程序文件就如一座大型的代碼庫支持著大家的工作。

一 How to Debug VBA. Debugging VBA in Excel

如何調試VBA及在Excel中調試VBA的方法

Writing Visual Basic for Applications code is hard, but what about writing VBA code that works and to write it fast? Often I found many colleges struggling to get a few simple procedures to work. I was amazed that most of them preferred to keep at it, trying to pin down the one line of code the causes their issues, rather then spend a few minutes learning how to properly debug Excel code and get the job done much faster! Introducing today’s post on how to debug VBA code!

編寫 Visual Basic for Applications 代碼很困難,但是如何編寫有效的VBA代碼並能快速編寫呢?我經常發現許多大學都在努力讓一些簡單的程序發揮作用。我很驚訝他們中的大多數人更願意堅持,試圖找出導致他們問題的一行代碼,而不是花幾分鐘學習如何正確調試Excel代碼並更快地完成工作!今天介紹關於如何調試VBA代碼的!

Debugging is essentially a methodical process of locating and fixing bugs (or defects as some prefer). Basically find that bug and kill it! Excel VBA compared to other programming languages / environments has one significant advantage – you can Debug code on the fly without having to recompile the code. This makes life much easier and debugging some much more pleasant! So let’s jump right to it.

調試本質上是定位和修復錯誤(或某些人喜歡稱之為缺陷)並使之能有條不紊的運行的過程。基本過程就是找到那個錯誤並殺死它! Excel VBA 與其他程式語言/環境相比有一個顯著優勢——您可以即時調試代碼,而無需重新編譯代碼。這使我們的工作更輕鬆,調試更愉快!所以讓我們直接開始吧。

1 DEBUG VBA: BASIC TERMS

調試VBA:基本術語

First we need to introduce some basic terms to facilitate the remaining part of this post:

首先,我們需要介紹一些基本術語,以方便本文的其餘部分:

Executing/Running code – the process of running a macro

執行/運行代碼 – 運行宏的過程

Debugging code– the process of finding and fixing bugs/defects

調試代碼 – 查找和修復錯誤/缺陷的過程

Breakpoint – a line of code at which the execution of the macro will pause

斷點 – 宏執行將暫停的一行代碼

2 RUNNING / BREAKING / RESETING

運行/斷開/復位

Let’s start with the tool bar at the top of the VBA Project Viewer window. You should find 3 buttons as shown below:

讓我們從 VBA 項目查看器窗口頂部的工具欄開始。您應該找到3個按鈕,如下所示:

講解。如果您有了VBA的一定基礎可以根據自己的需要,進行教程的選擇及工具

The buttons allow you to do the following:

這些按鈕允許您執行以下操作:

Run – run your macro (Sub) or UserForm. This is equivalent to the key shortcut F5

運行 – 運行宏(子)或用戶窗體。這相當於按鍵快捷鍵 F5

Break – pause a running macro +. You can also stop a running macro by hitting the buttonCTRLBreakEsc

中斷 – 暫停正在運行的宏 +。您還可以通過點擊按鈕來停止正在運行的宏CTRL Break Esc

Reset – reset a running/paused macro

重置 – 重置正在運行/暫停的宏

These are the basic commands for running macros.

這些是運行宏的基本命令。

3 BREAK POINTS

斷點

Breakpoints specify lines of code at which the execution of your macro should pause when you debug VBA. They are convenient when you want to be sure your code does run through a certain loop of If statement.

斷點指定調試 VBA 時宏執行應暫停的代碼行。當您想要確保代碼確實通過 If 語句的某個循環運行時,它們非常方便。

,第1是入門後的提高,第3字典是必備的VBA之精華,第9是實用的典型案例

To add/remove a breakpoint simply left-click on the left gray bar in your VBA Project View next to your code. A red dot should appear indicating that you have specified a new breakpoint. Click on the dot again to remove the breakpoint.

要添加/刪除斷點,只需在 VBA 項目視圖中代碼旁邊的左側灰色欄上單擊滑鼠左鍵即可。應出現一個紅點,指示您已指定新的斷點。再次單擊該點可刪除斷點。

Assertions – the right way to breakpoint errors

Assertions – 解決斷點錯誤的正確方法

Often breakpoints are specified in places where error might occur. This may be cumbersome when you have loop running and are not sure when the error will occur or if you are aware of a condition that causes the error but are unable to catch it at the right moment. This is where you will want to use Debug.Assert.

通常在可能發生錯誤的位置指定斷點。當您正在運行循環並且不確定錯誤何時會發生時,或者如果您意識到導致錯誤但無法在正確的時刻捕獲它的情況,這可能會很麻煩。這是您需要使用 Debug.Assert 的地方。

How does Debug.Assert work? Say you are dividing to numbers and want to make sure the denominator is non-zero. Otherwise you want the code to pause. Consider the example below. In the first example the code will continue to execute normally, in the second example however the macro will immediately pause at the assertion as if a breakpoint was defined!

Debug.Assert 如何工作?假設您正在除以數字,並希望確保分母為非零。否則,您希望代碼暫停。請考慮下面的示例。在第一個示例中,代碼將繼續正常執行,但是在第二個示例中,宏將立即在斷言處暫停,就像定義了斷點一樣!

Sub mynzA()

x = 100

y = 10

Debug.Assert y <> 0 'Condition met: Continue!


x = 120

y = 0

Debug.Assert y <> 0 'Condition false!: Pause!

End Sub

對於學員而言,如果您是入門階段可以打包選擇7139教程,第7是入門

運行結果:

好了,可以大大提高自己的勞動效率,而且可以提高數據的準確度。

4 STEPPING THROUGH CODE

單步執行代碼

The key to debugging is to skillfully step through your code either by line or an entire function/procedure. Here are the basic commands found in the menu toolbar:Debug

調試的關鍵是熟練地通過行或整個函數/過程單步執行代碼。以下是菜單工具欄中的基本命令:Debug

用技術。正如我給VBA的定義:VBA是個人小型自動化處理的有效工具。利用

Step Into F8 – step into each procedure/function

單步F8 – 單步執行每個過程/函數

Step Over SHIFT+F8 – step over every procedure/function (run just the current procedure)

單步SHIFT+F8 – 單步執行每個過程/函數(僅運行當前過程)

Step Out CTRL+SHIFT+F8 – step out of the current running procedure

單步執行 CTRL+SHIFT+F8 – 單步執行當前正在運行的過程

Run to Cursor CTRL+F8 – execute and break at the line pointed by the cursor

運行到光標 CTRL+F8 – 在光標指向的行處執行並斷開

Usually this is enough although you might want to get familiar with the other commands in the menu toolbar.Debug

通常,這就足夠了,儘管您可能希望熟悉菜單工具欄中的其他命令。Debug

5 THE IMMEDIATE WINDOW AND DEBUG.PRINT

「即時」窗口和「調試.列印」

In the bottom left corner of VBA editor you should find the Immediate window. This panel can be used to execute immediately pieces of code (even your code is paused). Simply start typing and hit ! Additionally the Immediate window is the default output of the Debug.Print VBA command which prints a certain provided string (similarly like the MsgBox but does not display any pop-up). The Debug.Print command is very convenient for outputting VBA execution messages / statuses or execution progress (e.g. number of processed items). ENTER

在VBA編輯器的左下角,您應該找到「即時」窗口。此面板可用於立即執行代碼段(即使您的代碼已暫停)。只需開始打字並點擊!此外,「即時」窗口是 Debug.Print VBA 命令的默認輸出,該命令列印某個提供的字符串(類似於 MsgBox,但不顯示任何彈出窗口)。Debug.Print 命令對於輸出 VBA 執行消息/狀態或執行進度(例如,已處理項的數量)非常方便。ENTER

Sub mynzB()

Debug.Print "Hello there!"

End Sub

The output:

輸出:

6 SUMMARY

總結

Debugging is an easy skill to learn. Knowing how to skillfully debug VBA code with benefit your coding experience and efficiency! Let me know what you think!

調試是一項易於學習的技能。了解如何熟練調試VBA代碼,並有益於您的編碼經驗和效率!

【分享成果,隨喜正能量】

術工具系列在不斷推出。從這些教程及工具中,大家可以看到VBA確實是一門實

我20多年的VBA實踐經驗,全部濃縮在下面的各個教程中:

百鍊成精,淡定從容。。VBA系列教程共十套,我已經全部完成,技
以,人一定要禁得起假話,受得住敷衍,忍得住欺騙,忘得了承諾,放得下一切,
【分享成果,隨喜正能量】有一些人的出現,就是來給我們開眼的。所

分享成果,隨喜正能量】往者不諫,來者可追。人生的珍貴,在於做過了,就無法重來;過去了,就無法在挽回。在合適的時間,做正確的事,不後悔、不懊悔,就是頂級的自律。。

聲明:文章觀點僅代表作者本人,PTTZH僅提供信息發布平台存儲空間服務。
喔!快樂的時光竟然這麼快就過⋯
繼續其他精彩內容吧!
more