Writing VBA Code
Writing VBA Code
CONTACT SIGN IN
01457 858877
Wise Owl Training Blogs Introduction to Visual Basic for Applications (VBA) Writing VBA Code (Visual Basic for Applciations)
BLOGS BY TOPIC If you've heard people in your office talking about macros or VBA but
don't understand what they mean, this blog series will make everything
Blog home page (346) clear. It's an introduction to the most basic skills you'll need to start
Excel + (122) creating your own programs in any of the Microsoft Office applications.
SQL Server (148)
1. Introduction to VBA (Visual Basic for Applications)
Programming (138)
2. Creating Your First VBA Program
General (22)
3. Writing VBA Code (Visual Basic for Applciations) (this blog)
Archived (70)
4. Running a Subroutine in VBA macros
5. Problems When Running VBA Code
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
BLOGS BY AUTHOR
BLOGS BY YEAR
2010 (2)
2011 (76)
2012 (100)
Writing VBA Code
2013 (45) If you've been following this blog series so far you should have a newly
created subroutine with a flashing text cursor poised and ready to write some
2014 (38)
real code. This article will explain some of the basic rules of writing VBA and
2015 (23) show you a couple of tricks to get you creating code as quickly as possible.
2016 (52)
2017 (10)
In VBA terms, the Thing part of the above sentence would technically be
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
called either a collection or an object. The Action part of the sentence would
be referred to as a method. So, the technical way of representing the above
sentence would look like this:
Object.Method
or
Collection.Method
Bearing this in mind, we're going to write a line of code that will apply theAdd
method to the Workbooks collection.
After typing in the full stop you should see a list of keywords appear
automatically. This feature is referred to as Intellisense - horrible name, useful
feature! The next section describes several ways to use Intellisense to save
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
you as much typing as possible.
Option Effect
Probably the quickest technique to use in this example is to type in the letter
A which will automatically select the word Add in the list, and then press
Enter .
Congratulations, you've
finally written your first line
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
of code! When we get
around to running our
subroutine, this line will
create a new workbook.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
In PowerPoint, the collection of open files is called Presentations.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Referring to a Range object is slightly more complicated than referring to the
Workbooks collection because we have to say which cell we are interested in.
We do this by typing a cell reference (here it isA1) inside a set of round
brackets and double-quotes. Next, we can type in a full stop to display the list
of properties and methods.
The quickest way to select the Value property from the list is to do the
following:
1. Type in the letter V to jump to the word Validation.
2. Press the down arrow key on the keyboard to select Value.
3. Press Tab .
We can now say what text we want to appear in the cell. To do this we need
to type in an equals sign, = followed by the text. All literal text in VBA must
be enclosed in a set of double-quotes. Type in the following and press Enter
at the end of the line.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
When you press Enter at the end of the line you should see a space
appear on either side of the equals sign.
As a final flourish in our very basic program, we're going to write a line that will
put today's date into another cell on the spreadsheet. This line will look very
similar to the one we've just created, so type in the following code. Try to
remember the quickest way to use the Intellisense list - hint: it doesn't involve
the mouse!
Press Enter at the end of the line and you should see the worddate
becomes capitalised.
Rather than putting in the date as a string of literal text, we've used a built-in
VBAfunction called Date. This function calculates what today's date is each
time the code is run (based on your computer's clock) and puts the result of
the calculation into the cell.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Wise Owl Business Solutions Ltd 2017. All Rights Reserved Sitemap Terms & Conditions Privacy policy
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com