Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>End:</strong></p> <blockquote> <p>Terminates execution immediately. Never required by itself but may be placed anywhere in a procedure to end code execution, close files opened with the Open statement and to clear variables. When executed, the End statement resets all module-level variables and all static local variables in all modules.</p> </blockquote> <p><strong>ThisWorkbook module:</strong></p> <pre><code>Public fileSystem As FileSystemObject Public errorStream As TextStream Private Sub Workbook_Open() Set fileSystem = New FileSystemObject Set errorStream = fileSystem.CreateTextFile("c:\temp\error.log", True) End Sub </code></pre> <p><strong>Standard module:</strong></p> <pre><code>Public Sub First() If (Not ThisWorkbook.errorStream Is Nothing) Then Debug.Print VBA.TypeName(ThisWorkbook.errorStream) End If End ' Exit Sub End Sub Public Sub Second() If (Not ThisWorkbook.errorStream Is Nothing) Then Debug.Print VBA.TypeName(ThisWorkbook.errorStream) End If End Sub </code></pre> <p>When 'First' method executes first with 'End' in it and then 'Second' method, then errorStream will be Nothing. Instaead of 'End' use 'ExitSub', then the variable will not be reset.</p> <p>Or you could make error-stream variable private in Thisworkbook class module and add property, which will create the stream if the variable is Nothing. HTH</p> <p><strong>ThisWorkbook module:</strong></p> <pre><code>Private m_errorStream As TextStream Private Const FILE_PATH_NAME As String = "c:\temp\error.log" Public Property Get ErrorStream() As TextStream If (m_errorStream Is Nothing) Then Dim fileSystem As FileSystemObject Set fileSystem = New FileSystemObject If (fileSystem.FileExists(FILE_PATH_NAME)) Then Set m_errorStream = fileSystem.GetFile(FILE_PATH_NAME).OpenAsTextStream Else Set m_errorStream = fileSystem.CreateTextFile(FILE_PATH_NAME, False) End If End If Set ErrorStream = m_errorStream End Property </code></pre> <p><strong>Standard module:</strong></p> <pre><code>Public Sub First() If (Not ThisWorkbook.ErrorStream Is Nothing) Then Debug.Print VBA.TypeName(ThisWorkbook.ErrorStream) End If End End Sub Public Sub Second() If (Not ThisWorkbook.ErrorStream Is Nothing) Then Debug.Print VBA.TypeName(ThisWorkbook.ErrorStream) End If End Sub </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload