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 onclickquick keys in excelranking chart excelvba activecell.rowadd developer tab to ribbon in microsoft wordbutton excel vbavba programs excel examplesexcel 2007 dashboard tutorialexcel remove hyperlinkfor loops excelmicrosoft excel hyperlinkhow to record a macro in excelwhat is the purpose of vlookupms office 2007 interview questions answers pdfexcel pivot graphexcel vba delete rowsconcatenate using vbavlookup trimmerge cells excelselect sheet vbaproject portfolio management template exceldashboards in excel 2007vba execute sqllearning excel vbavba convertervba range commanddefine name in excel 2007checkbox in excellistbox columnaverageif excel 2010trim functionproject management checklist excelhow to unhide columns in excel 2007remove vbasample vba code for excelcountifs formula in excelamazing excel macrosexcel vba copy range to another worksheetgetopenfilename vbac programming interview questions answers and explanationsvba code to read excel filedml and ddl in sql serverpivot table excel 2007 tutorialbreak password protected excelmacro excel tutorialprotect worksheet in excelword vba checkboxdelete a row vbaclose form vbafrequently asked sql interview questionsrefresh pivot table in vbaiserr excelvba concatenationlookup excel examplesdelete rows in excelhow to find duplicates in excel 2003fence estimate templatevba compare datesvlookup excel function examplesexcel chart add inshyperlink in excelrun query in access vbaadding a drop down in excelsql equality operatorsintroduction to vba for excelhow to insert checkbox in excel 2007excel vba global variablesexcel delete row vbaexcel select duplicatesmacro excel for dummiesmultiple project dashboard template excelvb messagebox