Fasten VBA Code – Application Objects Explained with Examples

Home/Excel VBA/Fasten VBA Code – Application Objects Explained with Examples

Here are the example codes to control the Events, Screen Updating, Progress Bar, Display Alerts and other Application Objects. This will help you to write Optimized VBA Code to fasten your code and build better VBA Macro Applications.

PREMIUM TEMPLATES LIMITED TIME OFFER

ON SALE80% OFF

50+ Project Management Templates Pack
Excel PowerPoint Word

Advanced Project Plan & Portfolio Template
Excel Template

Business Presentations Templates Pack
PowerPoint Slides

20+ Excel Project Management Pack
Excel Templates

20+ PowerPoint Project Management Pack
PowerPoint Templates

10+ MS Word Project Management Pack
Word Templates


Stop Screen Updating

You can fasten your code by stopping your screen updating. You can use the following syntax to disable Screen Updating.

To Disable Screen Updating:

Application.ScreenUpdating = False

To Enable Screen Updating:

Application.ScreenUpdating = True

Note: All ways it is a better practice to enable the screen updating once you done with your task.

Example: To print 50,000 numbers

Sub PrintNos()
Dim i As Long
Application.ScreenUpdating = False
For i = 1 To 50000
Sheets("Sheet1").Cells(i, 1) = i
Next
Application.ScreenUpdating = True
End Sub

Stop Events in Excel VBA – Disable Enable

You can disable the events by using Application.EnableEvents = False

Example: The following code will stop the events and change the value of combo box

Sub sbStopEvents()
'Diable Events
Application.EnableEvents = False
'Do your Task
'changing combo box
combo1.Value = "VBA Tutorials"
'Make sure that you are enabling the events once you are done with your task
Application.StatusBar = ""
End Sub

Stop Application Alerts in Excel VBA – Disable Enable

You can suppress the Waning alerts by disabling the display alerts.

Situation 1: When you are deleting a sheet using VBA, Excel popup a warning message.
Situation 2: When you are saving a file with already existing file name, Excel popup a warning message.

To avoid this, here is the solution:

To Enable display alerts:

Application.DisplayAlerts = True

To Disable display alerts:

Application.DisplayAlerts = False

Note: All ways it is a better practice to Enable the alerts once you are done with the suppressing the alerts.

Example 1: When saving a workbook

Sub ExampleSaveWorkbook()
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs ThisWorkbook.Path & "\Output.xls"
Application.DisplayAlerts = True
End Sub

Example 2: When deleting a worksheet

Sub ExampleDeleteSheet()
Application.DisplayAlerts = False
ActiveSheet.Delete
Application.DisplayAlerts = True
End Sub

Display Progress on Statusbar in VBA Excel

Solution: You can display the progress on the stausbar using Application.Stausbar property
Example: The following code will print the 5000 numbers and show you the progress on the statusbar

Sub sbStatusBar()
'Pring some text on statusbar
Application.StatusBar = "Start Printing the Numbers"
'Print numbers
For icntr = 1 To 5000
Cells(icntr, 1) = icntr
'Printing the Progress
Application.StatusBar = " Please wait while printing the numbers " & round((icntr / 5000 * 100),0) & "%"
Next
'Make sure that you are clearing the Status bar once you are done with your task
Application.StatusBar = ""
End Sub

Set Windows State in Excel VBA – Minimize Maximize Normal

Sub sbWindowState()
'Maximized State
Application.WindowState = xlMaximized
'Minimized State
Application.WindowState = xlMinimized
'Normal State
Application.WindowState = xlNormal
End Sub

Toggle Full Screen in Excel VBA

Sub sbFullScreen()
'Full Screen Mode
Application.DisplayFullScreen = True
'Normal Mode
Application.DisplayFullScreen = False
End Sub

Get User Name in VBA Excel

Sub sbGetUserName()
MsgBox Application.UserName
End Sub

Stop Calculations in Excel VBA – Manual Automatic

Sub sbStopCalculations()
Application.Calculation = xlCalculationManual
'Your code here.
Application.Calculation = xlCalculationAutomatic
End Sub

Open Visual Basic Editor (VBE) – Open Module with VBA

You can use Goto method

'Open Visual Basic Editor
Sub sbOpenVBE()
With Application
'To Open a sub procedure 'sbOpenVBE'
.Goto "sbOpenVBE"
End With
End Sub

VBA to Exit from Procedure or Function

Example: The following code will Exit from the code based on a condition

Sub sbTerminateProc()
'Terminate the procedure if the a cell value is equals to 155.5
For iCntr=1 to 10000
If Cells(iCntr,1)=155.5 then Exit Sub
Next
End Sub
LIMITED TIME OFFER
By |February 10th, 2013|Excel VBA|0 Comments

About the Author:

Excel VBA Developer having around 8 years of experience in using Excel and VBA for automating the daily tasks, reports generation and dashboards preparation. Valli is sharing to helps us automating daily tasks.

Leave A Comment


Related pages


vba find last rowisblank functionsas analyst interview questionshlookup excel 2010worksheets vba excelexcel remove rows containingodbc vbasyntax excel definitionautosave excel 2007use excel to find duplicatesexcel vba color codescreate button vbacopy and paste macroexcel vba windowmerge data from multiple worksheetshow to capture data from userform into excel worksheetv lookup h lookup pivot tablerange excel macrohow to use iferror function in excel 2010how to hide lines in excelsql data manipulation commandsvelocap excelexcel macro cell valuevba excel tutorialdelete entire row vbaremove blank rows excelhow do you sort columns in excelexcel 2007 all formulas with exampleshow to convert excel to xmlexcel nested vlookupexample sumiftechnical aptitude test questions and answersvba rgbvba read text filewhat is the vlookup functioncustomer service dashboard excelvb code for excelexcel combining worksheetsvba elseexcel dashboard templatesddl and dml statements in sqlexcel vba close formmsdasql downloadexcel pie chart examplesshortcut keys for microsoft excelspider web chart excelvba do loopsvba listbox selected itemexcel vba deleteexcel vba gotovlookup excel 2007 tutorialexcel template checklisthow do you unhide a column in excelfso.movefiletesting interview questions and answers for freshersvba userform initializevba lessonsexcel check for nullexcel vba on cell changevba excel sql queryproject estimation excel templateiferror excelvb6 interview questionsvba macros in excel 2007range macro excelhow to use sumifs in excelvba form loadexcel vba case statementmatching cells in excelinsert a checkbox in excel 2010ddl and dmlvba to open excel fileexcel loop functionconnect excel to sql database