VISUAL BASIC: the basics

Now that you know the meaning of VBA, let's sea what it can do......

It can do Every......wait for it!!! ..... Thing....... IT can DO EVERYTHING......

Why? Because it is a programming language and therefore it will do what you tell it to do.

IT can do useful things like having a button moving us from one spreadsheet to another..... it can do a timer like in the example here.

But it can be fun too, like here..... (you have to accept the macro to see it work). this macro will make your screen dance, the best is to watch it at night try it here.

How to begin:

The easiest way to start with Visual Basic is to create a MACRO.

Let's do it step by step.

We are going to create a first easy funny but useful macro.  AAHHHH but I forgot to tell you what a macro is.....

A macro is a recorded series of commands that you can execute by calling the macro. The macro can be called via a button for example. This is what we are going to see here under. Macros are written in Visual Basic. So recording a macro creates automatically VB code.

First you have to activate the developer ribbon. This is done here.

Create a new Workbook and create 2 spreadsheets.

spreadsheet

becomes

spreadsheet

Go back to Sheet 1.

You might not have the developper ribbon. So you can activate it in the option menu

developer ribbon

 

Now we are going to insert a Button around the cell A1. For this press the Insert Tab in the DEVELOPER RIBBON.

INSERT TAB

Then press the top left item, which is a button.

The cursor will change into a cross.

Draw a rectangle around the cell A1.

As soon as you release the mouse button, the following window will open.

macro1

This window allows you to assign macros to elements like buttons for example and also to record new MACRO.....so PRESS RECORD......

The following windows opens:

macro recording

Enter a comment if you wish and press OK.

Then the window disappearsand press the Sand press the Spreadsheet 2 tab at the bottom in order to move the this sheet and press the STOP RECORDING BUTTON.

stop recording macro button excel

Now you should have a button called Button1 in the sheet 1.

button in sheet excel

Now GO to Sheet 2 and do exactly the same.

Create a button, a draw it into the cell A1.

create button

You can see the window, lists the old button1_click macro and the new one will be called Button2_click.

Press RECORD...

record

Enter a comment if you wish and press OK.

1) Then the window disappears

2) Press the Spreadsheet 1 tab at the bottom in order to move to this sheet

3) Then press the STOP RECORDING BUTTON.

stop recording macro button excel

You will have a new button 2 in sheet 2.

button 2 sheet 2 excel

Now press ESCape in order to deselect the button.

 

RESULT:

if you press the button 1 it goes to sheet 2 and if you press the button 2 it goes to sheet 1......

THIS WAS YOU FIRST MACRO...... and by the way, there is visual basic code behind.....

You can see it here.

By pressing the MACRO button in the DEVELOPER RIBBON.

macro button developer ribbon

Press the Edit button in the following windows to make the code appear.

EDIT MACRO EXCEL

You can see the code highlighted

code vba

Or here. Even the Comments you entered are seen in the code.

 

Sub Button1_Click()
'
' Button1_Click Macro
' goes to sheet 2
'
Sheets("Sheet2").Select

End Sub
Sub Button2_Click()
'
' Button2_Click Macro
' goes to sheet 1


Sheets("Sheet1").Select
End Sub

The code was really very simple coding but this was your first VBA. Congratulation.

You can download this example here.

Congratulation for your first "automated VBA code generation".....