SlideShare a Scribd company logo
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
Ad

More Related Content

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
Phil Eugene
 
Microsoft Office 2003 Creating Macros
Microsoft Office 2003 Creating MacrosMicrosoft Office 2003 Creating Macros
Microsoft Office 2003 Creating Macros
S Burks
 
My power point macros
My power point macrosMy power point macros
My power point macros
Javier Morales Cauna
 
Vba 2 (students copy)
Vba 2 (students copy)Vba 2 (students copy)
Vba 2 (students copy)
Sherwin Keith Rivera
 
Salesforce Admin Hacks
Salesforce Admin HacksSalesforce Admin Hacks
Salesforce Admin Hacks
Joshua 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 labs
Jagadish a
 
Chapter 9
Chapter 9Chapter 9
Chapter 9
application developer
 
VISUAL
VISUALVISUAL
VISUAL
emi kenye
 
REmote server
REmote serverREmote server
REmote server
Darnette A
 
Excel 2007 Unit I
Excel 2007 Unit IExcel 2007 Unit I
Excel 2007 Unit I
Raja Waseem Akhtar
 
Lesson2
Lesson2Lesson2
Lesson2
Vincent Elmalih
 
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 Macros
S Burks
 
Salesforce Admin Hacks
Salesforce Admin HacksSalesforce Admin Hacks
Salesforce Admin Hacks
Joshua 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 labs
Jagadish a
 

Viewers also liked (10)

Question words (wh & how)
Question words (wh & how)Question words (wh & how)
Question words (wh & how)
Marcos_L
 
Tecnologia educativa
Tecnologia educativaTecnologia educativa
Tecnologia educativa
soleflor
 
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
Malik Ross
 
Deals2nitefinalpresentation
Deals2nitefinalpresentationDeals2nitefinalpresentation
Deals2nitefinalpresentation
BobHalo21
 
Statement of purpose
Statement of purposeStatement of purpose
Statement of purpose
kapil baral
 
Sample SoP for MBA Application Sample
Sample SoP for MBA Application SampleSample SoP for MBA Application Sample
Sample SoP for MBA Application Sample
MBA Statement of Purpose Samples
 
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
 
Mpls basic
Mpls basicMpls basic
Mpls basic
Ahmed Hussien Ali Gomaa Bebars
 
El bullying!
El bullying!El bullying!
El bullying!
Abel0700
 
Lectura,carte...încotro?
Lectura,carte...încotro?Lectura,carte...încotro?
Lectura,carte...încotro?
Lucretia Birz
 
Question words (wh & how)
Question words (wh & how)Question words (wh & how)
Question words (wh & how)
Marcos_L
 
Tecnologia educativa
Tecnologia educativaTecnologia educativa
Tecnologia educativa
soleflor
 
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
Malik Ross
 
Deals2nitefinalpresentation
Deals2nitefinalpresentationDeals2nitefinalpresentation
Deals2nitefinalpresentation
BobHalo21
 
Statement of purpose
Statement of purposeStatement of purpose
Statement of purpose
kapil 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
 
Ad

Similar to VBA Tips (20)

Autocad excel vba
Autocad excel vbaAutocad excel vba
Autocad excel vba
rjg_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 Delhi
ibinstitute0
 
VT University Live Session 3
VT University Live Session 3VT University Live Session 3
VT University Live Session 3
VisibleThread
 
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
ibinstitute0
 
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
Er. 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 reports
quest2900
 
Lab3 RTC Source Control
Lab3 RTC Source ControlLab3 RTC Source Control
Lab3 RTC Source Control
IBM Rational software
 
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
guest38bf
 
VBA
VBAVBA
VBA
Rohit Garg
 
Spreadsheet Analytical Tools
Spreadsheet Analytical ToolsSpreadsheet Analytical Tools
Spreadsheet Analytical Tools
Joselito 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.docx
nettletondevon
 
Excel Vba Basic Tutorial 1
Excel Vba Basic Tutorial 1Excel Vba Basic Tutorial 1
Excel Vba Basic Tutorial 1
rupeshkanu
 
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
Dan D'Urso
 
An introduction to vba and macros
An introduction to vba and macrosAn introduction to vba and macros
An introduction to vba and macros
Er. Nawaraj Bhandari
 
008.module
008.module008.module
008.module
Học Huỳnh Bá
 
ASP.NET MVC3 RAD
ASP.NET MVC3 RADASP.NET MVC3 RAD
ASP.NET MVC3 RAD
Mădălin Ștefîrcă
 
Vb6.0 intro
Vb6.0 introVb6.0 intro
Vb6.0 intro
JOSEPHINEA6
 
How To Automate Part 3
How To Automate Part 3How To Automate Part 3
How To Automate Part 3
Sean 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.pdf
Khushal Chate
 
Debugger & Profiler in NetBeans
Debugger & Profiler in NetBeansDebugger & Profiler in NetBeans
Debugger & Profiler in NetBeans
Huu Bang Le Phan
 
Autocad excel vba
Autocad excel vbaAutocad excel vba
Autocad excel vba
rjg_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 Delhi
ibinstitute0
 
VT University Live Session 3
VT University Live Session 3VT University Live Session 3
VT University Live Session 3
VisibleThread
 
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
ibinstitute0
 
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
Er. 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 reports
quest2900
 
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
guest38bf
 
Spreadsheet Analytical Tools
Spreadsheet Analytical ToolsSpreadsheet Analytical Tools
Spreadsheet Analytical Tools
Joselito 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.docx
nettletondevon
 
Excel Vba Basic Tutorial 1
Excel Vba Basic Tutorial 1Excel Vba Basic Tutorial 1
Excel Vba Basic Tutorial 1
rupeshkanu
 
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
Dan D'Urso
 
How To Automate Part 3
How To Automate Part 3How To Automate Part 3
How To Automate Part 3
Sean 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.pdf
Khushal Chate
 
Debugger & Profiler in NetBeans
Debugger & Profiler in NetBeansDebugger & Profiler in NetBeans
Debugger & Profiler in NetBeans
Huu Bang Le Phan
 
Ad

Recently uploaded (20)

Google DeepMind’s New AI Coding Agent AlphaEvolve.pdf
Google DeepMind’s New AI Coding Agent AlphaEvolve.pdfGoogle DeepMind’s New AI Coding Agent AlphaEvolve.pdf
Google DeepMind’s New AI Coding Agent AlphaEvolve.pdf
derrickjswork
 
Cybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and MitigationCybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and Mitigation
VICTOR MAESTRE RAMIREZ
 
UiPath AgentHack - Build the AI agents of tomorrow_Enablement 1.pptx
UiPath AgentHack - Build the AI agents of tomorrow_Enablement 1.pptxUiPath AgentHack - Build the AI agents of tomorrow_Enablement 1.pptx
UiPath AgentHack - Build the AI agents of tomorrow_Enablement 1.pptx
anabulhac
 
Slack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teamsSlack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teams
Nacho Cougil
 
Top Hyper-Casual Game Studio Services
Top  Hyper-Casual  Game  Studio ServicesTop  Hyper-Casual  Game  Studio Services
Top Hyper-Casual Game Studio Services
Nova Carter
 
Agentic Automation - Delhi UiPath Community Meetup
Agentic Automation - Delhi UiPath Community MeetupAgentic Automation - Delhi UiPath Community Meetup
Agentic Automation - Delhi UiPath Community Meetup
Manoj Batra (1600 + Connections)
 
React Native for Business Solutions: Building Scalable Apps for Success
React Native for Business Solutions: Building Scalable Apps for SuccessReact Native for Business Solutions: Building Scalable Apps for Success
React Native for Business Solutions: Building Scalable Apps for Success
Amelia Swank
 
IT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information TechnologyIT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information Technology
SHEHABALYAMANI
 
Who's choice? Making decisions with and about Artificial Intelligence, Keele ...
Who's choice? Making decisions with and about Artificial Intelligence, Keele ...Who's choice? Making decisions with and about Artificial Intelligence, Keele ...
Who's choice? Making decisions with and about Artificial Intelligence, Keele ...
Alan Dix
 
Building a research repository that works by Clare Cady
Building a research repository that works by Clare CadyBuilding a research repository that works by Clare Cady
Building a research repository that works by Clare Cady
UXPA Boston
 
論文紹介:"InfLoRA: Interference-Free Low-Rank Adaptation for Continual Learning" ...
論文紹介:"InfLoRA: Interference-Free Low-Rank Adaptation for Continual Learning" ...論文紹介:"InfLoRA: Interference-Free Low-Rank Adaptation for Continual Learning" ...
論文紹介:"InfLoRA: Interference-Free Low-Rank Adaptation for Continual Learning" ...
Toru Tamaki
 
DNF 2.0 Implementations Challenges in Nepal
DNF 2.0 Implementations Challenges in NepalDNF 2.0 Implementations Challenges in Nepal
DNF 2.0 Implementations Challenges in Nepal
ICT Frame Magazine Pvt. Ltd.
 
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Maarten Verwaest
 
Building the Customer Identity Community, Together.pdf
Building the Customer Identity Community, Together.pdfBuilding the Customer Identity Community, Together.pdf
Building the Customer Identity Community, Together.pdf
Cheryl Hung
 
OpenAI Just Announced Codex: A cloud engineering agent that excels in handlin...
OpenAI Just Announced Codex: A cloud engineering agent that excels in handlin...OpenAI Just Announced Codex: A cloud engineering agent that excels in handlin...
OpenAI Just Announced Codex: A cloud engineering agent that excels in handlin...
SOFTTECHHUB
 
Refactoring meta-rauc-community: Cleaner Code, Better Maintenance, More Machines
Refactoring meta-rauc-community: Cleaner Code, Better Maintenance, More MachinesRefactoring meta-rauc-community: Cleaner Code, Better Maintenance, More Machines
Refactoring meta-rauc-community: Cleaner Code, Better Maintenance, More Machines
Leon Anavi
 
Secondary Storage for a microcontroller system
Secondary Storage for a microcontroller systemSecondary Storage for a microcontroller system
Secondary Storage for a microcontroller system
fizarcse
 
IT488 Wireless Sensor Networks_Information Technology
IT488 Wireless Sensor Networks_Information TechnologyIT488 Wireless Sensor Networks_Information Technology
IT488 Wireless Sensor Networks_Information Technology
SHEHABALYAMANI
 
Dark Dynamism: drones, dark factories and deurbanization
Dark Dynamism: drones, dark factories and deurbanizationDark Dynamism: drones, dark factories and deurbanization
Dark Dynamism: drones, dark factories and deurbanization
Jakub Šimek
 
ICDCC 2025: Securing Agentic AI - Eryk Budi Pratama.pdf
ICDCC 2025: Securing Agentic AI - Eryk Budi Pratama.pdfICDCC 2025: Securing Agentic AI - Eryk Budi Pratama.pdf
ICDCC 2025: Securing Agentic AI - Eryk Budi Pratama.pdf
Eryk Budi Pratama
 
Google DeepMind’s New AI Coding Agent AlphaEvolve.pdf
Google DeepMind’s New AI Coding Agent AlphaEvolve.pdfGoogle DeepMind’s New AI Coding Agent AlphaEvolve.pdf
Google DeepMind’s New AI Coding Agent AlphaEvolve.pdf
derrickjswork
 
Cybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and MitigationCybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and Mitigation
VICTOR MAESTRE RAMIREZ
 
UiPath AgentHack - Build the AI agents of tomorrow_Enablement 1.pptx
UiPath AgentHack - Build the AI agents of tomorrow_Enablement 1.pptxUiPath AgentHack - Build the AI agents of tomorrow_Enablement 1.pptx
UiPath AgentHack - Build the AI agents of tomorrow_Enablement 1.pptx
anabulhac
 
Slack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teamsSlack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teams
Nacho Cougil
 
Top Hyper-Casual Game Studio Services
Top  Hyper-Casual  Game  Studio ServicesTop  Hyper-Casual  Game  Studio Services
Top Hyper-Casual Game Studio Services
Nova Carter
 
React Native for Business Solutions: Building Scalable Apps for Success
React Native for Business Solutions: Building Scalable Apps for SuccessReact Native for Business Solutions: Building Scalable Apps for Success
React Native for Business Solutions: Building Scalable Apps for Success
Amelia Swank
 
IT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information TechnologyIT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information Technology
SHEHABALYAMANI
 
Who's choice? Making decisions with and about Artificial Intelligence, Keele ...
Who's choice? Making decisions with and about Artificial Intelligence, Keele ...Who's choice? Making decisions with and about Artificial Intelligence, Keele ...
Who's choice? Making decisions with and about Artificial Intelligence, Keele ...
Alan Dix
 
Building a research repository that works by Clare Cady
Building a research repository that works by Clare CadyBuilding a research repository that works by Clare Cady
Building a research repository that works by Clare Cady
UXPA Boston
 
論文紹介:"InfLoRA: Interference-Free Low-Rank Adaptation for Continual Learning" ...
論文紹介:"InfLoRA: Interference-Free Low-Rank Adaptation for Continual Learning" ...論文紹介:"InfLoRA: Interference-Free Low-Rank Adaptation for Continual Learning" ...
論文紹介:"InfLoRA: Interference-Free Low-Rank Adaptation for Continual Learning" ...
Toru Tamaki
 
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Maarten Verwaest
 
Building the Customer Identity Community, Together.pdf
Building the Customer Identity Community, Together.pdfBuilding the Customer Identity Community, Together.pdf
Building the Customer Identity Community, Together.pdf
Cheryl Hung
 
OpenAI Just Announced Codex: A cloud engineering agent that excels in handlin...
OpenAI Just Announced Codex: A cloud engineering agent that excels in handlin...OpenAI Just Announced Codex: A cloud engineering agent that excels in handlin...
OpenAI Just Announced Codex: A cloud engineering agent that excels in handlin...
SOFTTECHHUB
 
Refactoring meta-rauc-community: Cleaner Code, Better Maintenance, More Machines
Refactoring meta-rauc-community: Cleaner Code, Better Maintenance, More MachinesRefactoring meta-rauc-community: Cleaner Code, Better Maintenance, More Machines
Refactoring meta-rauc-community: Cleaner Code, Better Maintenance, More Machines
Leon Anavi
 
Secondary Storage for a microcontroller system
Secondary Storage for a microcontroller systemSecondary Storage for a microcontroller system
Secondary Storage for a microcontroller system
fizarcse
 
IT488 Wireless Sensor Networks_Information Technology
IT488 Wireless Sensor Networks_Information TechnologyIT488 Wireless Sensor Networks_Information Technology
IT488 Wireless Sensor Networks_Information Technology
SHEHABALYAMANI
 
Dark Dynamism: drones, dark factories and deurbanization
Dark Dynamism: drones, dark factories and deurbanizationDark Dynamism: drones, dark factories and deurbanization
Dark Dynamism: drones, dark factories and deurbanization
Jakub Šimek
 
ICDCC 2025: Securing Agentic AI - Eryk Budi Pratama.pdf
ICDCC 2025: Securing Agentic AI - Eryk Budi Pratama.pdfICDCC 2025: Securing Agentic AI - Eryk Budi Pratama.pdf
ICDCC 2025: Securing Agentic AI - Eryk Budi Pratama.pdf
Eryk Budi Pratama
 

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
  翻译: