VBA HOME PAGE

Menu

Recording macros

Looking at the code

Ways of running macros

Where macros are stored

Reasons to write macros

Writing macros

Procedure types

Visual Basic editor (VBE)

Rules & conventions

Excel objects

Range/Selection objects

Object hierarchy

Object browser

Chart objects

Pivot Table objects

Formulas

Visual Basic Functions

Creating Add-Ins

Variables & constants

Object variables

Arrays

Collections

Message Box

VBA Input Box

Excel Input Box

Making decisions (If)

Making decisions (Case)

Looping (Do...Loop)

Looping (For...Loop)

With...End With blocks

User defined functions

Event handling

Error handling

Debugging

Creating User Forms

DAO/ADO Objects

Input/Output Files

 

Other links

Example code snippets

Userform input example

 

 

 

 

 


Writing macros

Start by writing a task list of the step you wish to capture and use this as your checklist to help cover all actions required and in the correct order.

 Define your task

1. Define the task you wish to program.

2. The overall task must be broken down into smaller tasks.

3. The program consists of a set of instructions or code, which the computer will follow.

4. The order in which you place these statements is very important.

 Layout of procedures

Declaration Area

 

Procedure Starts Name()

      1st line of Statements ‘Comments

      2nd line of Statements ‘Comments

      ..........

End of Procedure

 

The blue text represents the procedure starting and ending signatures

The green text represents the narrative/comments for documentation purposes which are excluded from the procedure.

All procedures must have starting signature and ending signature.

 Pseudo code

Write the program out in plain English to explain what is going to happen.

                    sub formatting()

                    bold

                    italic

                    underline

                    end sub

 

 Calling a procedure (formatting)

                    sub start()

                    select cells A2:A10

                    formatting

                    select cells B1:G1

                    formatting

                    end sub 

 Writing macros from scratch

The following macro will select Sheet 1 and type January into cell A1 and 100 into cell A2.

 

1. Create a new blank workbook.

2. Click on the Developer tab, click Visual Basic icon.

3. In the VBE window, click on the Insert menu and select Module.

Pseudo Code

VBA Code

Sub january()

Select Sheet1

Select the cell A1

Type January

Select the cell A2

Type 100

End Sub

Sub january()

Worksheets("Sheet1").Select

Range("A1").Select

ActiveCell.Value = "January"

Range(“A2”).Select

ActiveCell.Value = 100

End Sub

 

4. Type the VBA code into the module and test.

 

Note that the text 'January' is entered with speech (double-quote) marks, as it is a piece of text, where as the number 100 is entered without.

 

 


Next Topic: Procedure types

 

Want to teach yourself Access? Free online guide at About Access Databases

Tip: Alt + F11 function keys switches between Excel and VBE window.

VBA Keywords: ActiveCell, Range, Worksheets, Select.

 
Home | Terms of Use | Privacy Policy | Contact

© copyright 2010 TP Development & Consultancy Ltd, All Rights Reserved.

All trademarks are copyrighted by their respective owners. Please read our terms of use and privacy policy.