天天看點

代碼調試過程中的斷點Break Points

作者:VBA語言専攻

【分享成果,随喜正能量】人世間就這樣,池塘大了,水就深了,水深了,魚就多了,大魚小魚,泥鳅黃鳝,烏龜王八,螃蟹龍蝦,鮮的腥的,臊的臭的,什麼貨色都有。與其抱怨這個世界不美好,不如自己努力,美好和幸運自然不期而至。。

我給VBA下的定義:VBA是個人小型自動化處理的有效工具。可以大大提高自己的勞動效率,而且可以提高資料的準确性。我這裡專注VBA,将我多年的經驗彙集在VBA系列九套教程中。

作為我的學員要利用我的積木程式設計思想,積木程式設計最重要的是積木如何搭建及擁有積木。在九套教程中我給出了大量的積木,同時講解了如何搭建。為了讓學員擁有更多的積木,我開始着手這部《VBA即用型代碼手冊(漢英)》的創作,這部手冊約600頁,集合約500多個的案例,案例我用漢語和英語同時釋出,一方面學員從中可以更好的領會和掌握VBA中用到的一些英語知識,另一方面,大家可以看到各種各樣的積木。這部手冊是大家學習和工作中的不可多得的實用資料。今日的内容是:代碼調試過程中的斷點Break Points

代碼調試過程中的斷點Break Points

3 斷點Break Points

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

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.

代碼調試過程中的斷點Break Points

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

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.

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

Assertions – the right way to breakpoint errors

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

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!

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

代碼調試過程中的斷點Break Points

運作結果:

代碼調試過程中的斷點Break Points

(待續)

代碼調試過程中的斷點Break Points

【分享成果,随喜正能量】我20多年的VBA實踐經驗,全部濃縮在下面的各個教程中:

代碼調試過程中的斷點Break Points

【分享成果,随喜正能量】有些人受恩惠久了,容易從最初的感激,變成理所當然,而偶爾的相助,反而會讓他記得一輩子。​​。

繼續閱讀