Thursday, 22 January 2009

creating a code module

There are lots of sources of information about using excel VBA and I really don't want to repeat it all here. But as a very very quick primer this is how to open the VB editor then create and run a really simply piece of code. This is for Excel 2002. Excel 2007 is slightly different in how you get to the editor but hey, just plug VBA into excel 2007 help and it will tell you.

So, creating your first piece of code: Load excel. Make sure there is a worbook loaded. Save it with a name you will remember. Lets say 'tradetest.xls'

Load the VBE editor. (Either go tools -> Macros -> Visual Basic Editor, or press Alt F11)


You should then see the VBE Editor with a menu structure screen on the left showing current projects and current modules. Right click over the VBA Project(tradetest.xls) , click insert, module.


You should now have a screen into which you can enter your code which should look something like this:


Now lets enter our first piece of code. Type the following into the code window:
________________________
Sub test()
'This a test piece of code

MsgBox "hello trader"

End sub
_______________________

Okay, so this not exactly exciting but you have to start somewhere.

To run the code click on the play button on the VB toolbar.



And you should get a message box in excel like this:


Well done, you have cracked your first piece of code.

No comments:

Post a Comment