Thursday, July 27, 2006

Recording our first Excel VBA Macro!

Recording Macros in Excel isn't difficult at all... and the great thing is, we can start learning a bit about programming in Excel by getting Excel to do the programming for us!
The Excel macro recorder is the best teacher that you can have and will remain the best assistant for the rest of your life.
Follow these instructions to create your first Macro:

  1. Start the Macro recorder
  2. In Cell A1, enter the value 25
  3. In Cell A2, enter the value 42
  4. And in A3, enter 56
  5. Set the font-size of A3 to 16.
  6. Set the background color of A2 to yellow.
  7. Stop the recorder.
Once we've recorded our Macro, see if you can find a way to look at the code that you've created! Can you understand the code?

After you've tested your first Macro a couple of times, see if you can make the following modifications to your code:
  • Modify your code so that the value that is put into cell A2 is 999 instead of 42, re-run your macro to test it!
  • Modify your code so that the font-size of cell A3 is set to 8 instead of 16, and again, re-run your macro.
  • Modify your code so that the background colour of cell A2 is something other than Yellow. As usual, re-run your macro to make sure that your change worked!
  • Challenge: See if you can add a button to your worksheet so that when you click on your button, your macro runs!
  • Save your worksheet as VBAMacreExamples.xls
Remember if you get stuck, you can always find help nearby! We'll also take a look at a few other features of VBA - such as the VBA Editor, the VBA Debugger, With - End With statements and using buttons to trigger macros. We'll also take a look at some of the vocabularly we'll be using while learning VBA. These are all tools that we'll be using and re-using over the next term!

Some points you will want to file away in your mind:
  • What does VBA stands for?
  • Where or why is it used in Excel (and other applications)?
  • How did you get to the VBA editor from Excel?
  • What's the easiest way to figure out how write the code to do something in Excel VBA?
  • Why is Debugging useful?

0 Comments:

Post a Comment

<< Home