How to get started with Excel VBA
description
Transcript of How to get started with Excel VBA
![Page 1: How to get started with Excel VBA](https://reader036.fdocuments.us/reader036/viewer/2022081420/56815ee1550346895dcd88b4/html5/thumbnails/1.jpg)
How to get started with Excel VBA
![Page 2: How to get started with Excel VBA](https://reader036.fdocuments.us/reader036/viewer/2022081420/56815ee1550346895dcd88b4/html5/thumbnails/2.jpg)
We need to enable programming in Excelthe “Developer menu”
![Page 3: How to get started with Excel VBA](https://reader036.fdocuments.us/reader036/viewer/2022081420/56815ee1550346895dcd88b4/html5/thumbnails/3.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022081420/56815ee1550346895dcd88b4/html5/thumbnails/4.jpg)
Make sure the Developer is ticked
![Page 5: How to get started with Excel VBA](https://reader036.fdocuments.us/reader036/viewer/2022081420/56815ee1550346895dcd88b4/html5/thumbnails/5.jpg)
Go to DeveloperVisual Basic
![Page 6: How to get started with Excel VBA](https://reader036.fdocuments.us/reader036/viewer/2022081420/56815ee1550346895dcd88b4/html5/thumbnails/6.jpg)
Programming window opens up, right click on Sheet1 and choose “Insert Form”
![Page 7: How to get started with Excel VBA](https://reader036.fdocuments.us/reader036/viewer/2022081420/56815ee1550346895dcd88b4/html5/thumbnails/7.jpg)
Like so
![Page 8: How to get started with Excel VBA](https://reader036.fdocuments.us/reader036/viewer/2022081420/56815ee1550346895dcd88b4/html5/thumbnails/8.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022081420/56815ee1550346895dcd88b4/html5/thumbnails/9.jpg)
Click view toolbox insert image
![Page 10: How to get started with Excel VBA](https://reader036.fdocuments.us/reader036/viewer/2022081420/56815ee1550346895dcd88b4/html5/thumbnails/10.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022081420/56815ee1550346895dcd88b4/html5/thumbnails/11.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022081420/56815ee1550346895dcd88b4/html5/thumbnails/12.jpg)
Drag out a command button and call CmdLightoff and write turn me off for the caption
![Page 13: How to get started with Excel VBA](https://reader036.fdocuments.us/reader036/viewer/2022081420/56815ee1550346895dcd88b4/html5/thumbnails/13.jpg)
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
![Page 14: How to get started with Excel VBA](https://reader036.fdocuments.us/reader036/viewer/2022081420/56815ee1550346895dcd88b4/html5/thumbnails/14.jpg)
Add the following code
Make the imglightoff visible
Make the imglighton visible
![Page 15: How to get started with Excel VBA](https://reader036.fdocuments.us/reader036/viewer/2022081420/56815ee1550346895dcd88b4/html5/thumbnails/15.jpg)
Draw out a label at the top of the form and call it lblmessage, leave the caption blank
![Page 16: How to get started with Excel VBA](https://reader036.fdocuments.us/reader036/viewer/2022081420/56815ee1550346895dcd88b4/html5/thumbnails/16.jpg)
Add the following code lblmessage.Caption = “turn me off now” in the Cmdlightson_Click button and run the program and
![Page 17: How to get started with Excel VBA](https://reader036.fdocuments.us/reader036/viewer/2022081420/56815ee1550346895dcd88b4/html5/thumbnails/17.jpg)
Press Turn me off and Turn me on
![Page 18: How to get started with Excel VBA](https://reader036.fdocuments.us/reader036/viewer/2022081420/56815ee1550346895dcd88b4/html5/thumbnails/18.jpg)
The following should be displayed
![Page 19: How to get started with Excel VBA](https://reader036.fdocuments.us/reader036/viewer/2022081420/56815ee1550346895dcd88b4/html5/thumbnails/19.jpg)
Try adding this to change the background colour of the image
![Page 20: How to get started with Excel VBA](https://reader036.fdocuments.us/reader036/viewer/2022081420/56815ee1550346895dcd88b4/html5/thumbnails/20.jpg)
![Page 21: How to get started with Excel VBA](https://reader036.fdocuments.us/reader036/viewer/2022081420/56815ee1550346895dcd88b4/html5/thumbnails/21.jpg)
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?