VBA (Visual Basic for Applications) is a programming language very much like Visual Basic, but embedded within an individual Microsoft application (e.g. Excel or Access are most common). You basically create macros, or small programs that perform tasks within their host application.

The intent of this page is to show some useful VBA  examples that I have picked up in the process of creating my own applications. Click on the bulleted items below to view VBA codes and tips.

Hide the Process of Executing Macro

The code below hides the process of executing a macro:

Application.ScreenUpdating = False
 'code here
Application.ScreenUpdating = True

Back to Top^

Line Break in VBA Message Box (MsgBox)

Use  "vbCrLf " or "vbNewLine" to create line breaks within a message box as shown below:

Msgbox ("This is Line #1" & vbNewLine & "This is Line #2" & vbNewLine & vbNewLine & "There are two lines above this one")

Back to Top^

Message Box Icons

Can I change the icons within a message box (MsgBox)?

Private Sub Form_Load()
 MsgBox "The Device was not Found!", 48, "Header"
End Sub

The above code produces the following message box:

"48" is the value of the assigned icon, which is a an exclamation mark. You can also substitute the value with the icon on message "vbExclamation". See the table of icons below.

Icon on message Value Short Description
vbCritical 16 Displays critical message icon
vbQuestion 32 Displays question icon
vbExclamation 48 Displays exclamation icon
vbInformation 64 Displays information icon

Back to Top^

Prevent Screen Flicker

Sometimes when you run a macro, the screen flickers a lot due to the screen updating itself. This slows the macro done especially when the macro has a lot of work to do. To prevent screen flickering, use the code below:

Application.ScreenUpdating = False

NOTE: Don't forget to turn the Screen Flicker back on! You need to set the screen updating back to true at the end of the macro.

Back to Top^

Print Form

If you want to print the Userform you can add this code to a button on the form:

Private Sub CommandButton1_Click()
End Sub

Back to Top^