EXCEL VBA
Legal Notice
Copyright (c) 2020 Anton Melnyk.
All rights are reserved. No portion of this book may be reproduced or duplicated using any form whether mechanical, electronic, or otherwise. No portion of this book may be transmitted, stored in a retrieval database, or otherwise made available in any manner whether public or private unless specific permission is granted by the publisher.
This book does not offer advice, but merely provides information. The author offers no advice whether medical, financial, legal, or otherwise, nor does the author encourage any person to pursue any specific course of action discussed in this book. This book is not a substitute for professional advice. The reader accepts complete and sole responsibility for the manner in which this book and its contents are used. The publisher and the author will not be held liable for any damages caused.
Excel VBA
Step by Step Guide to Learn Excel VBA Programming
with Screenshots and Example Code
Anton Melnyk
Contents
Excel VBA
VBA stands for Visual Basic for Applications and is the programming language used in all office programs (Excel, Word, Outlook, etc.).
This programming language can be used to create all kinds of automation tools and to create relationships between Office programs move data from one tool to another.
This book will show you how you can harness some of the power of Excel VBA for yourself.
Working with the Visual Basic Editor
Visual Basic Editor is the interface where the code can be written. To open Visual Basic Editor, you need to follow this steps:
1. Click on the Developer tab from the top ribbon menu of any Outlook program.
If the Developer tab is not visible you have to right click on Home button and click on Customize Quick Access Toolbar. Select Customize Ribbon option on the left pane and check Developer checkbox in the right pane.
2. Click on the Visual Basic button
OR
Press Alt + F11 buttons from the keyboard on Windows configuration or Opt + F11 buttons from the keyboard on MAC configuration.
After the Visual Basic Editor is open there can be found multiple sections described below:
Menu Bar is the main menu of the Visual Basic Editor. From the Menu Bar you can
- save your project
- use functions as find or replace
- select what windows to be visible (Immediate Window, Immediate Window, Watches Window, etc.)
- insert modules, procedures or user forms
- format your code (align, horizontal or vertical spacing, arrange buttons, etc.)
- debug your code by: step into (run the code line by line), step over (skip some lines of code when running), add a variable to the watch window (check the variable value while running the code)
- run the code, break the code or stop the code
- add some references. References are very important when the code uses some other applications. For example, if you want to include some Word references you need to add Microsoft Word as a reference.
- include some Add-ins
- change Window view
- get some help if is needed
All the commands described above have also keyboard shortcuts.
Tool Bar contains most of the useful commands that are used while codding. This toolbar can be customized with most needed commands.
Edit Toolbar Buttons:
- List Properties / Methods (CTRL + J) When something from the code (Sub, Function, Variable, Method, etc.) is clicked and this option pressed shows a list of all properties and methods of that object from the code
- List Constants (CTRL + Shift + J) Displays all the constants available in the code
- Quick Info (CTRL + I) - When something from the code (Sub, Function, Variable, Method, etc.) is clicked and this option pressed shows a list of information of that object from the code
- Parameter Info (CTRL + Shift + I) Displays all the parameter that are needed after click on a Sub/Function name in the code
- Complete Word (CTRL + Space) Complete the word if its already recognized by VBA it some letters are typed in
- Indent / Outdent (TAB / Shift + TAB After some code text is selected this option will shift the selected code to the next or previous TAB
- Toggle Breakpoint (F9) Inserting a breakpoint in the selected line
- Toggle Bookmark Inserting a bookmark in the selected line
- Next Bookmark Move the cursor to the next bookmark in the code
- Previous Bookmark Move the cursor to the previous bookmark in the code
- Clear All Bookmarks Remove all bookmarks from the code
- Comment Block Comment the selected text (used for commenting multiples lines of code at once)
- Uncomment Block Uncomment the selected text
Standard Toolbar Buttons:
- View Microsoft Excel (Alt + F11) Switch to Microsoft Excel application from Visual Basic Editor
- Insert User Form Allows the user to insert new User Form, Module, Class Module or Procedures
- Save (CTRL + S) Saves the progress (including Excel Workbook and Visual Basic Macro)
- Cut (CTRL + X) Standard cut functionality
- Copy (CTRL + C) Standard copy functionality
- Paste (CTRL + V) Standard paste functionality
- Find (CTRL + F) Standard find functionality
- Undo (CTRL + Z) Reverse the last action
- Redo Restores the last action
- Run (F5) Runs the current part of the code. If the cursor is inside a Sub or Function, it will run the Sub or Function and it stops only if there is a Breakpoint in the code. If a User Form is open in the Visual Basic Editor, then it will run the User Form
- Break (CTRL + Break) Pause the execution of the code and switch to break mode (in this way the code can be run line by line)
- Reset / Stop Stops the execution of the code, resets the project and clear all variables/clipboard items/objects
- Design Mode Switch to design mode view (edit buttons / labels / text boxes, etc.)
- Project Explorer (CTRL + R) Displays the Project Explorer window on the screen
- Properties Window (F4) Displays the Properties window on the screen
- Object Browser (F2) Displays the Object browser on the screen
- Toolbox Displays toolbox when a User Form is active. This toolbox is used to insert new items on the User Form (Text, Dropdowns, Images, Buttons, Tables, etc.)
- Help (F1) Displays the Office assistant window
Debug Toolbar Buttons:
- Design Mode Switch to design mode view (edit buttons / labels / text boxes, etc.)
- Run/Continue(F5) Runs the current part of the code. If the cursor is inside a Sub or Function, it will run the Sub or Function and it stops only if there is a Breakpoint in the code. If a User Form is open in the Visual Basic Editor, then it will run the User Form
- Break (CTRL + Break) Pause the execution of the code and switch to break mode (in this way the code can be run line by line)
- Reset / Stop Stops the execution of the code, resets the project and clear all variables/clipboard items/objects
- Toggle Breakpoint (F9) Inserting a breakpoint in the selected line
- Step Into (F8) Runs the next line of code which is executable and steps into procedures or functions (this is helpful if some actions are done inside some procedures or functions because the code is run step by step)