SlideShare a Scribd company logo
1 of 6
Download to read offline
Bonus Chapter 2
TenVBATipsandTricks
In This Chapter
ᮣ Using helpful habits
ᮣ Making your work more efficient
This chapter contains a list of ten clever tricks I’ve developed (or acquired
from other users) over the years.
Getting VBA Help, Fast
When working in a VBA module, you can get instant help regarding a VBA
object, property, or method. Just move the cursor to the word that interests
you and press F1.
Speeding Up Your Macros
If you write a VBA macro that produces lots of on-screen action, you can
speed things up significantly by turning off screen updating. To do so, exe-
cute this statement:
Application.ScreenUpdating = False
If your macro uses a custom dialog box, make sure to turn screen updating
back on before displaying the UserForm. Otherwise, moving the dialog box
on the screen leaves an ugly trail.
2 Excel VBA Programming For Dummies
Avoiding Excel’s Questions
Some VBA methods cause Excel to display a confirmation message,
which requires the user to click a button. For example, the statement
ActiveSheet.Delete always displays a dialog box that asks for confirmation.
To eliminate such confirmation messages, execute the following before the
statement that causes the confirmation messages.
Application.DisplayAlerts = False
Use this statement to reinstate the confirmation messages:
Application.DisplayAlerts = True
Displaying One Procedure at a Time
Normally, a Code window in the Visual Basic Editor (VBE) shows all the pro-
cedures in the module, one after another. If you find this distracting, set
things up so that only one procedure is visible.
1. Activate the VBE and choose Tools➪Options.
2. Click the Editor tab in the Options dialog box.
3. Remove the check mark from the Default to Full Module View
check box.
Then you can use the drop-down lists at the top of the module window to
select the procedure to view or edit.
Using With-End With
If you need to set a number of properties for an object, your code is easier to
read and faster running if you use the With-End With construct. The following
code doesn’t use With-End With:
Selection.HorizontalAlignment = xlCenter
Selection.VerticalAlignment = xlCenter
Selection.WrapText = True
Selection.Orientation = 0
Selection.ShrinkToFit = False
Selection.MergeCells = False
3Bonus Chapter 2: Ten VBA Tips and Tricks
The next code performs the same action but is rewritten to use With-End
With:
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.ShrinkToFit = False
.MergeCells = False
End With
Reducing the Size of a Workbook
In many cases, you can significantly reduce the size of a workbook —
especially a workbook with modules you’ve heavily edited — because Excel
does not do a good job of cleaning up after itself. To clean up the mess
Excel leaves behind:
1. Save your workbook.
2. Select a module or a UserForm in the Project Window.
3. Right-click and choose Remove from the shortcut menu.
4. When asked whether you want to export the module, click Yes.
5. Repeat Steps 3 and 4 for each module and UserForm, keeping track of
the modules and forms that you remove.
6. Choose File➪Import File to import all the modules and forms you
deleted.
7. Save your workbook again.
You usually find that the new workbook is much smaller than it was.
Another way to reduce the size of a workbook file is as follows:
1. Activate your workbook.
2. Choose File➪Save As Web Page, and make sure that you use the
Entire Workbook option.
3. Close your workbook.
4. Use File➪Open to open the HTML file that you saved in Step 2.
5. Use File➪Save As, and resave the workbook as a standard XLS file.
In most cases, you’ll find that the file is smaller in size.
4 Excel VBA Programming For Dummies
Bypassing a Workbook_Open Procedure
Workbook_Open is a macro that Excel executes automatically when you open
a workbook. In some situations, you may want to avoid running this macro.
To do so, press the Shift key while opening the workbook.
Using Your Personal Macro Workbook
If you’ve developed some general-purpose macros, consider storing them in
your Personal Macro Workbook, which opens automatically whenever Excel
starts. When you record a macro, you have the option of recording it to your
Personal Macro Workbook. The file, PERSONAL.XLS, is stored in your
XLSTART directory. The Personal Macro Workbook is created the first time
you record a macro to it.
The Personal Macro Workbook is hidden by default.
Displaying Messages in the Status Bar
If you develop a lengthy macro, use the Excel status bar to display text that
describes the progress of the macro. To do so, write some code that periodi-
cally executes another macro, such as the following:
Sub UpdateStatusBar(PctDone)
Application.StatusBar = _
“Percent Completed: “ & Format(PctDone, “0%”)
End Sub
This procedure uses one argument: a value that ranges from 0 to 1.0. The
macro simply displays a message that indicates the percent completed. To
return the status bar back to normal, execute the following statement:
Application.StatusBar = False
Forcing Yourself to Declare All Variables
Declaring every variable that you use in your code is an excellent practice.
For example, if you use an integer variable named Count, declare it as Dim
Count as Integer.
Declaring your variables as a particular data type makes your code run faster
and also helps avoid typographical errors. To force yourself to declare all
variables, insert the following statement at the top of your module:
Option Explicit
If you would like this statement automatically added to each new module,
follow these steps:
1. Activate the VBE and choose Tools➪Options.
2. In the Options dialog box, click the Editor tab.
3. Place a check mark next to Require Variable Declaration.
5Bonus Chapter 2: Ten VBA Tips and Tricks
6 Excel VBA Programming For Dummies

More Related Content

What's hot

RapidResponse Autoresponder Quick Start Guide Pictorial
RapidResponse Autoresponder Quick Start Guide Pictorial RapidResponse Autoresponder Quick Start Guide Pictorial
RapidResponse Autoresponder Quick Start Guide Pictorial Phil Eugene
 
Microsoft Office 2003 Creating Macros
Microsoft Office 2003 Creating MacrosMicrosoft Office 2003 Creating Macros
Microsoft Office 2003 Creating MacrosS Burks
 
Salesforce Admin Hacks
Salesforce Admin HacksSalesforce Admin Hacks
Salesforce Admin HacksJoshua Loomis
 
How to add the windows calculator to the quick access toolbar in microsoft ex...
How to add the windows calculator to the quick access toolbar in microsoft ex...How to add the windows calculator to the quick access toolbar in microsoft ex...
How to add the windows calculator to the quick access toolbar in microsoft ex...Trường Tiền
 
Using splunk6.2 labs
Using splunk6.2 labsUsing splunk6.2 labs
Using splunk6.2 labsJagadish a
 

What's hot (12)

RapidResponse Autoresponder Quick Start Guide Pictorial
RapidResponse Autoresponder Quick Start Guide Pictorial RapidResponse Autoresponder Quick Start Guide Pictorial
RapidResponse Autoresponder Quick Start Guide Pictorial
 
Microsoft Office 2003 Creating Macros
Microsoft Office 2003 Creating MacrosMicrosoft Office 2003 Creating Macros
Microsoft Office 2003 Creating Macros
 
My power point macros
My power point macrosMy power point macros
My power point macros
 
Vba 2 (students copy)
Vba 2 (students copy)Vba 2 (students copy)
Vba 2 (students copy)
 
Salesforce Admin Hacks
Salesforce Admin HacksSalesforce Admin Hacks
Salesforce Admin Hacks
 
How to add the windows calculator to the quick access toolbar in microsoft ex...
How to add the windows calculator to the quick access toolbar in microsoft ex...How to add the windows calculator to the quick access toolbar in microsoft ex...
How to add the windows calculator to the quick access toolbar in microsoft ex...
 
Using splunk6.2 labs
Using splunk6.2 labsUsing splunk6.2 labs
Using splunk6.2 labs
 
Chapter 9
Chapter 9Chapter 9
Chapter 9
 
VISUAL
VISUALVISUAL
VISUAL
 
REmote server
REmote serverREmote server
REmote server
 
Excel 2007 Unit I
Excel 2007 Unit IExcel 2007 Unit I
Excel 2007 Unit I
 
Lesson2
Lesson2Lesson2
Lesson2
 

Viewers also liked

Question words (wh & how)
Question words (wh & how)Question words (wh & how)
Question words (wh & how)Marcos_L
 
Tecnologia educativa
Tecnologia educativaTecnologia educativa
Tecnologia educativasoleflor
 
resume_Malik Ross_IV Pharmacy Technician Hospital-Home Infusion 053115
resume_Malik Ross_IV  Pharmacy Technician Hospital-Home Infusion 053115resume_Malik Ross_IV  Pharmacy Technician Hospital-Home Infusion 053115
resume_Malik Ross_IV Pharmacy Technician Hospital-Home Infusion 053115Malik Ross
 
Deals2nitefinalpresentation
Deals2nitefinalpresentationDeals2nitefinalpresentation
Deals2nitefinalpresentationBobHalo21
 
Statement of purpose
Statement of purposeStatement of purpose
Statement of purposekapil baral
 
FPGA FIR filter implementation (Audio signal processing)
FPGA FIR filter implementation (Audio signal processing)FPGA FIR filter implementation (Audio signal processing)
FPGA FIR filter implementation (Audio signal processing)Hocine Merabti
 
El bullying!
El bullying!El bullying!
El bullying!Abel0700
 
Lectura,carte...încotro?
Lectura,carte...încotro?Lectura,carte...încotro?
Lectura,carte...încotro?Lucretia Birz
 

Viewers also liked (10)

Question words (wh & how)
Question words (wh & how)Question words (wh & how)
Question words (wh & how)
 
Tecnologia educativa
Tecnologia educativaTecnologia educativa
Tecnologia educativa
 
resume_Malik Ross_IV Pharmacy Technician Hospital-Home Infusion 053115
resume_Malik Ross_IV  Pharmacy Technician Hospital-Home Infusion 053115resume_Malik Ross_IV  Pharmacy Technician Hospital-Home Infusion 053115
resume_Malik Ross_IV Pharmacy Technician Hospital-Home Infusion 053115
 
Deals2nitefinalpresentation
Deals2nitefinalpresentationDeals2nitefinalpresentation
Deals2nitefinalpresentation
 
Statement of purpose
Statement of purposeStatement of purpose
Statement of purpose
 
Sample SoP for MBA Application Sample
Sample SoP for MBA Application SampleSample SoP for MBA Application Sample
Sample SoP for MBA Application Sample
 
FPGA FIR filter implementation (Audio signal processing)
FPGA FIR filter implementation (Audio signal processing)FPGA FIR filter implementation (Audio signal processing)
FPGA FIR filter implementation (Audio signal processing)
 
Mpls basic
Mpls basicMpls basic
Mpls basic
 
El bullying!
El bullying!El bullying!
El bullying!
 
Lectura,carte...încotro?
Lectura,carte...încotro?Lectura,carte...încotro?
Lectura,carte...încotro?
 

Similar to VBA Tips

Autocad excel vba
Autocad excel vbaAutocad excel vba
Autocad excel vbarjg_vijay
 
Learn VBA Training & Advance Excel Courses in Delhi
Learn VBA Training & Advance Excel Courses in DelhiLearn VBA Training & Advance Excel Courses in Delhi
Learn VBA Training & Advance Excel Courses in Delhiibinstitute0
 
VT University Live Session 3
VT University Live Session 3VT University Live Session 3
VT University Live Session 3VisibleThread
 
Online Advance Excel & VBA Training in India
 Online Advance Excel & VBA Training in India Online Advance Excel & VBA Training in India
Online Advance Excel & VBA Training in Indiaibinstitute0
 
Using macros in microsoft excel part 2
Using macros in microsoft excel   part 2Using macros in microsoft excel   part 2
Using macros in microsoft excel part 2Er. Nawaraj Bhandari
 
Access tips access and sql part 6 dynamic reports
Access tips  access and sql part 6  dynamic reportsAccess tips  access and sql part 6  dynamic reports
Access tips access and sql part 6 dynamic reportsquest2900
 
AVB201.1 MS Access VBA Module 1
AVB201.1 MS Access VBA Module 1AVB201.1 MS Access VBA Module 1
AVB201.1 MS Access VBA Module 1guest38bf
 
Spreadsheet Analytical Tools
Spreadsheet Analytical ToolsSpreadsheet Analytical Tools
Spreadsheet Analytical ToolsJoselito Perez
 
Adding GetFormula to Your SpreadsheetsRachel Koh Spring 20.docx
Adding GetFormula to Your SpreadsheetsRachel Koh Spring 20.docxAdding GetFormula to Your SpreadsheetsRachel Koh Spring 20.docx
Adding GetFormula to Your SpreadsheetsRachel Koh Spring 20.docxnettletondevon
 
Excel Vba Basic Tutorial 1
Excel Vba Basic Tutorial 1Excel Vba Basic Tutorial 1
Excel Vba Basic Tutorial 1rupeshkanu
 
AVB201.2 Microsoft Access VBA Module 2
AVB201.2 Microsoft Access VBA Module 2AVB201.2 Microsoft Access VBA Module 2
AVB201.2 Microsoft Access VBA Module 2Dan D'Urso
 
How To Automate Part 3
How To Automate Part 3How To Automate Part 3
How To Automate Part 3Sean Durocher
 
003_AS1_Exercise_03_v1_0.pdf
003_AS1_Exercise_03_v1_0.pdf003_AS1_Exercise_03_v1_0.pdf
003_AS1_Exercise_03_v1_0.pdfKhushal Chate
 
Debugger & Profiler in NetBeans
Debugger & Profiler in NetBeansDebugger & Profiler in NetBeans
Debugger & Profiler in NetBeansHuu Bang Le Phan
 

Similar to VBA Tips (20)

Autocad excel vba
Autocad excel vbaAutocad excel vba
Autocad excel vba
 
Learn VBA Training & Advance Excel Courses in Delhi
Learn VBA Training & Advance Excel Courses in DelhiLearn VBA Training & Advance Excel Courses in Delhi
Learn VBA Training & Advance Excel Courses in Delhi
 
VT University Live Session 3
VT University Live Session 3VT University Live Session 3
VT University Live Session 3
 
Online Advance Excel & VBA Training in India
 Online Advance Excel & VBA Training in India Online Advance Excel & VBA Training in India
Online Advance Excel & VBA Training in India
 
Using macros in microsoft excel part 2
Using macros in microsoft excel   part 2Using macros in microsoft excel   part 2
Using macros in microsoft excel part 2
 
Access tips access and sql part 6 dynamic reports
Access tips  access and sql part 6  dynamic reportsAccess tips  access and sql part 6  dynamic reports
Access tips access and sql part 6 dynamic reports
 
Lab3 RTC Source Control
Lab3 RTC Source ControlLab3 RTC Source Control
Lab3 RTC Source Control
 
AVB201.1 MS Access VBA Module 1
AVB201.1 MS Access VBA Module 1AVB201.1 MS Access VBA Module 1
AVB201.1 MS Access VBA Module 1
 
VBA
VBAVBA
VBA
 
Spreadsheet Analytical Tools
Spreadsheet Analytical ToolsSpreadsheet Analytical Tools
Spreadsheet Analytical Tools
 
Adding GetFormula to Your SpreadsheetsRachel Koh Spring 20.docx
Adding GetFormula to Your SpreadsheetsRachel Koh Spring 20.docxAdding GetFormula to Your SpreadsheetsRachel Koh Spring 20.docx
Adding GetFormula to Your SpreadsheetsRachel Koh Spring 20.docx
 
Excel Vba Basic Tutorial 1
Excel Vba Basic Tutorial 1Excel Vba Basic Tutorial 1
Excel Vba Basic Tutorial 1
 
AVB201.2 Microsoft Access VBA Module 2
AVB201.2 Microsoft Access VBA Module 2AVB201.2 Microsoft Access VBA Module 2
AVB201.2 Microsoft Access VBA Module 2
 
An introduction to vba and macros
An introduction to vba and macrosAn introduction to vba and macros
An introduction to vba and macros
 
008.module
008.module008.module
008.module
 
ASP.NET MVC3 RAD
ASP.NET MVC3 RADASP.NET MVC3 RAD
ASP.NET MVC3 RAD
 
Vb6.0 intro
Vb6.0 introVb6.0 intro
Vb6.0 intro
 
How To Automate Part 3
How To Automate Part 3How To Automate Part 3
How To Automate Part 3
 
003_AS1_Exercise_03_v1_0.pdf
003_AS1_Exercise_03_v1_0.pdf003_AS1_Exercise_03_v1_0.pdf
003_AS1_Exercise_03_v1_0.pdf
 
Debugger & Profiler in NetBeans
Debugger & Profiler in NetBeansDebugger & Profiler in NetBeans
Debugger & Profiler in NetBeans
 

Recently uploaded

Navigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern EnterpriseNavigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern EnterpriseWSO2
 
Simplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxSimplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxMarkSteadman7
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard37
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAnitaRaj43
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Bhuvaneswari Subramani
 
Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...caitlingebhard1
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
How to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cfHow to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cfdanishmna97
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data PlatformLess Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data PlatformWSO2
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityWSO2
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Decarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational PerformanceDecarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational PerformanceIES VE
 
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....rightmanforbloodline
 
JavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate GuideJavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate GuidePixlogix Infotech
 

Recently uploaded (20)

Navigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern EnterpriseNavigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern Enterprise
 
Simplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxSimplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptx
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptx
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by Anitaraj
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
How to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cfHow to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cf
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data PlatformLess Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Decarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational PerformanceDecarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational Performance
 
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
 
JavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate GuideJavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate Guide
 

VBA Tips

  • 1. Bonus Chapter 2 TenVBATipsandTricks In This Chapter ᮣ Using helpful habits ᮣ Making your work more efficient This chapter contains a list of ten clever tricks I’ve developed (or acquired from other users) over the years. Getting VBA Help, Fast When working in a VBA module, you can get instant help regarding a VBA object, property, or method. Just move the cursor to the word that interests you and press F1. Speeding Up Your Macros If you write a VBA macro that produces lots of on-screen action, you can speed things up significantly by turning off screen updating. To do so, exe- cute this statement: Application.ScreenUpdating = False If your macro uses a custom dialog box, make sure to turn screen updating back on before displaying the UserForm. Otherwise, moving the dialog box on the screen leaves an ugly trail.
  • 2. 2 Excel VBA Programming For Dummies Avoiding Excel’s Questions Some VBA methods cause Excel to display a confirmation message, which requires the user to click a button. For example, the statement ActiveSheet.Delete always displays a dialog box that asks for confirmation. To eliminate such confirmation messages, execute the following before the statement that causes the confirmation messages. Application.DisplayAlerts = False Use this statement to reinstate the confirmation messages: Application.DisplayAlerts = True Displaying One Procedure at a Time Normally, a Code window in the Visual Basic Editor (VBE) shows all the pro- cedures in the module, one after another. If you find this distracting, set things up so that only one procedure is visible. 1. Activate the VBE and choose Tools➪Options. 2. Click the Editor tab in the Options dialog box. 3. Remove the check mark from the Default to Full Module View check box. Then you can use the drop-down lists at the top of the module window to select the procedure to view or edit. Using With-End With If you need to set a number of properties for an object, your code is easier to read and faster running if you use the With-End With construct. The following code doesn’t use With-End With: Selection.HorizontalAlignment = xlCenter Selection.VerticalAlignment = xlCenter Selection.WrapText = True Selection.Orientation = 0 Selection.ShrinkToFit = False Selection.MergeCells = False
  • 3. 3Bonus Chapter 2: Ten VBA Tips and Tricks The next code performs the same action but is rewritten to use With-End With: With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlCenter .WrapText = True .Orientation = 0 .ShrinkToFit = False .MergeCells = False End With Reducing the Size of a Workbook In many cases, you can significantly reduce the size of a workbook — especially a workbook with modules you’ve heavily edited — because Excel does not do a good job of cleaning up after itself. To clean up the mess Excel leaves behind: 1. Save your workbook. 2. Select a module or a UserForm in the Project Window. 3. Right-click and choose Remove from the shortcut menu. 4. When asked whether you want to export the module, click Yes. 5. Repeat Steps 3 and 4 for each module and UserForm, keeping track of the modules and forms that you remove. 6. Choose File➪Import File to import all the modules and forms you deleted. 7. Save your workbook again. You usually find that the new workbook is much smaller than it was. Another way to reduce the size of a workbook file is as follows: 1. Activate your workbook. 2. Choose File➪Save As Web Page, and make sure that you use the Entire Workbook option. 3. Close your workbook. 4. Use File➪Open to open the HTML file that you saved in Step 2. 5. Use File➪Save As, and resave the workbook as a standard XLS file. In most cases, you’ll find that the file is smaller in size.
  • 4. 4 Excel VBA Programming For Dummies Bypassing a Workbook_Open Procedure Workbook_Open is a macro that Excel executes automatically when you open a workbook. In some situations, you may want to avoid running this macro. To do so, press the Shift key while opening the workbook. Using Your Personal Macro Workbook If you’ve developed some general-purpose macros, consider storing them in your Personal Macro Workbook, which opens automatically whenever Excel starts. When you record a macro, you have the option of recording it to your Personal Macro Workbook. The file, PERSONAL.XLS, is stored in your XLSTART directory. The Personal Macro Workbook is created the first time you record a macro to it. The Personal Macro Workbook is hidden by default. Displaying Messages in the Status Bar If you develop a lengthy macro, use the Excel status bar to display text that describes the progress of the macro. To do so, write some code that periodi- cally executes another macro, such as the following: Sub UpdateStatusBar(PctDone) Application.StatusBar = _ “Percent Completed: “ & Format(PctDone, “0%”) End Sub This procedure uses one argument: a value that ranges from 0 to 1.0. The macro simply displays a message that indicates the percent completed. To return the status bar back to normal, execute the following statement: Application.StatusBar = False Forcing Yourself to Declare All Variables Declaring every variable that you use in your code is an excellent practice. For example, if you use an integer variable named Count, declare it as Dim Count as Integer.
  • 5. Declaring your variables as a particular data type makes your code run faster and also helps avoid typographical errors. To force yourself to declare all variables, insert the following statement at the top of your module: Option Explicit If you would like this statement automatically added to each new module, follow these steps: 1. Activate the VBE and choose Tools➪Options. 2. In the Options dialog box, click the Editor tab. 3. Place a check mark next to Require Variable Declaration. 5Bonus Chapter 2: Ten VBA Tips and Tricks
  • 6. 6 Excel VBA Programming For Dummies