MOCKSTACKS
EN
Questions And Answers

More Tutorials









VBA Procedure Names


Procedures do something. Name them after what they're doing, using a verb. If accurately naming a procedure is not possible, likely the procedure is doing too many things and needs to be broken down into smaller, more specialized procedures.

Some common VBA naming conventions go thus:

For all Procedures:


PascalCase

Public Sub DoThing()
End Sub
Private Function ReturnSomeValue() As [DataType]
End Function

For event handler procedures:


ObjectName_EventName

Public Sub Workbook_Open()
End Sub
Public Sub Button1_Click()
End Sub

Event handlers are usually automatically named by the VBE; renaming them without renaming the object and/or the handled event will break the code - the code will run and compile, but the handler procedure will be orphaned and will never be executed.

Boolean Members


Consider a Boolean-returning function:

Function bReadFile(ByVal strFile As String, ByRef strData As String) As Boolean
End Function

Compare to:


Function CanReadFile(ByVal path As String, ByRef outContent As String) As Boolean
End Function

The Can prefix does serve the same purpose as the b prefix: it identifies the function's return value as a Boolean. But Can reads better than b:

If CanReadFile(path, content) Then

Compared to:

If bReadFile(strFile, strData) Then

Consider using prefixes such as Can, Is or Has in front of Boolean-returning members (functions and properties),
but only when it adds value.

Conclusion

In this page (written and validated by ) you learned about VBA Procedure Names . What's Next? If you are interested in completing VBA tutorial, your next topic will be learning about: VBA Data Structures.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.