how to get started with excel vba. we need to enable programming in excel the “developer menu”

22
How to get started with Excel VBA

Upload: annabel-elliott

Post on 16-Dec-2015

218 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: How to get started with Excel VBA. We need to enable programming in Excel  the “Developer menu”

How to get started with Excel VBA

Page 2: How to get started with Excel VBA. We need to enable programming in Excel  the “Developer menu”

We need to enable programming in Excelthe “Developer menu”

Page 3: How to get started with Excel VBA. We need to enable programming in Excel  the “Developer menu”

Objectives 1) To understand the components of the VBA interface 2) Understand the difference between objects and properties3) To use these principles to effect changes in the behaviour of chosen objects4) To be able to get objects to move around the screen

Page 4: How to get started with Excel VBA. We need to enable programming in Excel  the “Developer menu”

Make sure the Developer is ticked

Page 5: How to get started with Excel VBA. We need to enable programming in Excel  the “Developer menu”

Go to DeveloperVisual Basic

Page 6: How to get started with Excel VBA. We need to enable programming in Excel  the “Developer menu”

Programming window opens up, right click on Sheet1 and choose “Insert Form”

Page 7: How to get started with Excel VBA. We need to enable programming in Excel  the “Developer menu”

Like so

Page 8: How to get started with Excel VBA. We need to enable programming in Excel  the “Developer menu”

Ensure that you save your work as an excel macro enabled workbook at this stage otherwise you will not be able to access your work again in VB

Page 9: How to get started with Excel VBA. We need to enable programming in Excel  the “Developer menu”

Click view toolbox insert image

Page 10: How to get started with Excel VBA. We need to enable programming in Excel  the “Developer menu”

Rename the image box Imglightsoff and import the picture of the light bulb which is off The border style property should be set to 0

Page 11: How to get started with Excel VBA. We need to enable programming in Excel  the “Developer menu”

Insert another image box and import the picture of the other light bulb which is switched on

Page 12: How to get started with Excel VBA. We need to enable programming in Excel  the “Developer menu”

Insert a command button and call it cmdlights and move the image of the light on to cover the image of the light off

Page 13: How to get started with Excel VBA. We need to enable programming in Excel  the “Developer menu”

Double-click on the form to enter the “code” window. Most things are typed between Sub and End Sub which are created for you by VB

Page 14: How to get started with Excel VBA. We need to enable programming in Excel  the “Developer menu”

Declaring a variable name as Boolean

Use the select if to turn the lightbulb on or off, When the lights is true the light bulb is on and when the lights are false the light bulb is turned off

End if part

Press F5 of the run command to execute the program

Changes the caption on the command button

Page 15: How to get started with Excel VBA. We need to enable programming in Excel  the “Developer menu”

Lights off Lights on

Page 16: How to get started with Excel VBA. We need to enable programming in Excel  the “Developer menu”

Activities

Try to get the light bulb when it is switched on to move randomly around the screen .

Page 17: How to get started with Excel VBA. We need to enable programming in Excel  the “Developer menu”

Insert the following code, this is called a procedure which only activates when the lights = true statement is true . The command calls this procedure and makes the lightbulb move around the screen

Page 18: How to get started with Excel VBA. We need to enable programming in Excel  the “Developer menu”
Page 19: How to get started with Excel VBA. We need to enable programming in Excel  the “Developer menu”

Add 3 scroll bars from the toolbox call them HSBRED, HSBGREEN and HSBBLUE. Change the maximum value on 255

Page 20: How to get started with Excel VBA. We need to enable programming in Excel  the “Developer menu”

Create a new procedure called sub showformcolour()Generates a random number between 0 and 255 Creates a random number to change the colour background of the form Sets the background colour of the form based on the 3 prime colours

Page 21: How to get started with Excel VBA. We need to enable programming in Excel  the “Developer menu”

Now run the program

Page 22: How to get started with Excel VBA. We need to enable programming in Excel  the “Developer menu”

Extension Activities

1) See if you can randomly change the background colour of each individual colour on the form.

2) Insert a message using a label 3) Insert a message using a msgbox