Skip to main content

Debug.Assert method

Table of contents
  1. Syntax
  2. Remarks
  3. Example

Conditionally suspends execution when booleanexpression returns False at the line on which the method appears.

Syntax

object.Assert booleanexpression

The Assert method syntax has the following object qualifier and argument:

Part Description
object Required. Always the Debug object.
booleanexpression Required. An expression that evaluates to either True or False.

Remarks

Assert invocations work only within the development environment. When the module is compiled into an executable, the method calls on the Debug object are omitted.

All of booleanexpression is always evaluated. For example, even if the first part of an And expression evaluates False, the entire expression is evaluated.

Example

The following example shows how to use the Assert method. The example requires two Command Button (ActiveX control) on sheet1. The default Command Button names are CommandButton1 and CommandButton2. Download Debug object Assert method Example.

When the workbook opens, clicking the CommandButton1 button toggles the text on the button between 0 and 1. Clicking CommandButton2 either does nothing or causes an assertion, depending on the value displayed on CommandButton1. The assertion stops execution with the last statement executed, the Debug.Assert line, highlighted.

'----------------- Sheet1 -----------------
Private blnAssert As Boolean
Private intNumber As Integer

Private Sub Worksheet_Activate()
    CommandButton1.Caption = intNumber
    CommandButton2.Caption = "Assert Tester"
End Sub

Private Sub CommandButton1_Click()
    blnAssert = Not blnAssert
    intNumber = IIf(intNumber <> 0, 0, 1)
    CommandButton1.Caption = intNumber
End Sub

Private Sub CommandButton2_Click()
    Debug.Assert blnAssert
End Sub

Leave a comment

Your email address will not be published. Required fields are marked *

Format your code: <pre><code class="language-vba">place your code here</code></pre>