Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.
With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company.
Learn more about Allen...
It is not unusual to create a macro that loads data from an external source (such as a database query) and then processes that data. If you create such a macro, you may notice a slight problem—Excel doesn't wait for the data refresh to complete before it begins merrily chunking away on the code that follows.
The reason for this is simple—when you refresh information in a workbook from an external source (such as an Oracle database query), Excel won't wait around. This is contrasted with internal events in Excel, which can be easily waited upon. To overcome this difference, you need to change the way you write the macros. Essentially, you need to write two separate macros. The first macro basically initiates the refresh from the external source, and the second macro is executed once the refresh is completed.
How do you know when you can run the second macro? You could do it manually after visually inspecting the worksheet to make sure everything loaded, but that ties you up. Instead, you can tie a macro to the AfterRefresh event. This event is triggered when (as its name suggests) the refresh is complete. For more information on how to use this event in your programming, visit the Microsoft Knowledge Base articles at these addresses:
These Knowledge Base articles are for Excel 97 and Excel 2000, but the information they contain will also work with later versions of Excel.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2612) applies to Microsoft Excel 97, 2000, 2002, and 2003.
Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!