How to get started with Excel VBA

Post on 15-Feb-2016

56 views 0 download

description

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 properties - PowerPoint PPT Presentation

transcript

How to get started with Excel VBA

We need to enable programming in Excelthe “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

Make sure the Developer is ticked

Go to DeveloperVisual Basic

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

Like so

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

Click view toolbox insert image

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

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

Drag out a command button and call CmdLightoff and write turn me off for the caption

Add another command button and call it Cmdlighton and add turn me on for the caption. Double click on the command button should take you to the code window

Add the following code

Make the imglightoff visible

Make the imglighton visible

Draw out a label at the top of the form and call it lblmessage, leave the caption blank

Add the following code lblmessage.Caption = “turn me off now” in the Cmdlightson_Click button and run the program and

Press Turn me off and Turn me on

The following should be displayed

Try adding this to change the background colour of the image

Extension Activities

1) Change the caption on the label when the light is switched on to read for the on the light “Hello World, you have turned me on” See you can change the back colour on the label to yellow using lblmessage.backcolor = vbyellow

2) Change the caption on the label to read “Goodbye world you have turned me off” See if you can change the background colour of the label to white

3) See if you can use the backcolour of the lightbulb when the light is turned out 4) See if you can change the caption that appears on the command button to

cmdlighton.caption to “don’t forget to turn me off when you are finished 5) What can be done to improve the program?