Does it ever seem awkward to run Visual Basic subroutines within Microsoft Excel? If you have a plain unvarnished subroutine, you must click through to the Developer tab, and then point and click to find the macro of interest. “There are more convenient ways to access the macros you build,” said Andrew Nikolishyn of Vesta Solutions. He was the instructor of a day-long seminar on “Visual Basic for Financial Professionals,” held on location at the CFA Society of Toronto on May 1, 2015.
In Part 1 of this posting, I described how participants could access the Developer tab to create and modify simple macros. Here are three more helpful things we covered.
Push a Button, Run the World
- To summon a macro directly from the Excel environment, click on the Developer tab.
- Choose Insert, and then choose the most appropriate button, a.k.a. Form Control, among the 12 Form Controls available. (See the Figure above)
- Once you select the button, give the button a name related to what the macro will do. (For example, “Clear the Page” or “Format the Page.”)
- You will be prompted to assign a macro to the button, so choose from the list given in the dialog box. Once you assign a button, every time you click on that button, it will carry out the macro—straight from the Excel page where you have placed the button.
Automate that Web Query
Nikolishyn showed an example web query.
- In Excel, click Data > From Web. Type in search “yahoo.com”. Click Go. You might get Script Error messages that ask “Do you want to continue running scripts?” Click No. [This happened to me several times.]
- Select the Finance link and look up AAPL (the stock ticker symbol for Apple).
- From the Summaries list (on the left hand side), click on Historical Prices.
- Select the data table by activating the arrow to a highlight green. Click the highlighted green box. (See Figure below.)
- The arrow will turn into a checkmark.
- In the lower right-hand screen, click the Import button.
- On the Excel worksheet, click in the cell where you want to place the upper-left corner of the web search.
- Note: at a later time, you can verify the web query by clicking Data > Existing Connections
A Fast Way to Select A Block of Data
Let’s say you’ve just executed the web query described in the preceding section. Next you want to select the entire block of data, perhaps to look for the maximum price. Nikolishyn pointed out there’s a fast way to “grab” that block of data.
- Record a macro that does the following: Make the worksheet active. Click on an occupied cell.
- Stop recording. Open the macro in the VB Editor.
- Add the line “Select Current Range” – that will select not just the occupied cell, but the entire contiguous data set.
These were three takeaway lessons from the seminar—three things that I plan to use.
The curriculum was ambitious for a one-day seminar, with various other topics covered:
- Creating a custom macro button on the Quick Access Toolbar;
- Stock-related example of properties and methods (sell stock if P/E triggers);
- Declaration of variables;
- Dynamic opening of a file or creation of file; and
- Copy/paste of a dynamic range.
Over all, I found the seminar was a superb refresher on VBA—from a financial analyst’s perspective, with small projects that could be taken back to the office and used right away.ª