Friday, September 15, 2006

Macros In Excel In recent thread at 3dbuzz, the poster of the thead ask about how to simulate a ball being dropped and bouncing on the floor. In my reply I included a Excel spreadsheet illustrating the simulation. In response to my reply I have been asked how I created the spreadsheet. So here I go... Macros are VBA scripts embedded in a Microsoft Office Document, they can be attached to cells, key commands and control objects. Here I only going to look at making a simple counter started by a buttons in Excel 2000. Open a new Excel document, right click on a menu and select Visual Basic. The icons left to right are: Run Macro, Record Marco, Security, Visual Basic Editor, Control Toolbox, Design Mode and Microsoft Script Editor. They are relatively self explanatory, so I'll just go over the ones we'll use, as we use them. Lets create a button, select Control Toolbox, then the button Icon in the Control Toolbox. If you notice, the Design Mode buttons are now selected and the a new toolbar has appeared. Only in design mode can you create and edit Controls. Now click somewhere on the sheet to create a button. To Change the buttons caption right click and highlight CommandButton Object and select Edit, and enter and caption you like. Now to get this button to do anything we need to start coding. Double click the button to bring up the VB editor. Now in the space type: Range("A1") = 0; while (Range("A1") <> Range("B1")) Range("A1") = Range("A1") + 1 DoEvents wend Going back to Excel, in cell B1 put in some large number like 123456789, exit design mode and click on your button. You should now see cell A1 quickly climb to the number you entered in cell B1. Now you've made a counter. Now to explain the code Range, is used to access a cells content. DoEvents, updates the current display. A few things you might what to try, create some forumlas that refer to cell A1. Create charts that refer to cell A1 and watch the dots move. I'll leave you now to play about with it. And I hope to be back soon with more you can do with macros in excel. thing2k

No comments: