This article was previously published under Q213187
This article has been archived. It is offered "as is" and will no longer be updated.
In Microsoft Excel, you can automatically run a macro before or after updating (refreshing) worksheet query data. You can do this with the Visual Basic for Applications BeforeRefresh and AfterRefresh events. This article contains an example of how to use the two events to run a macro.
Before you can use the sample macro, retrieve data from Microsoft Query toa Microsoft Excel worksheet. To create the sample data, follow these steps:
On the Data menu, point to Get External Data, and then click New Database Query.
On the Databases tab in the Choose Data Source dialog box, click <New Data Source>, click to clear the Use the Query Wizard to create/edit queries check box, and then click OK.
In the Create New Data Source dialog box, type Test in the first box that asks what you want to name the data source.
In the second box that asks you to select a database driver, select Microsoft dBase Driver (*.dbf).
In the ODBC dBase Setup dialog box, click to clear the Use Current Directory check box, and then click Select Directory.
In the Select Directory dialog box, locate the following folder:
Program Files\Microsoft Office\Office\1033
The Customer.dbf, Employee.dbf, and Orders.dbf files should be listed in this folder. If they are not, run the Office Setup program and install them.
Click OK in the ODBC dBase Setup dialog box.
Click OK in the Create New Data Source dialog box.
Select the Test data source in the Choose Data Source dialog box, and then click OK.
Microsoft Query starts and displays the Add Tables dialog box.
Click Customer.dbf, click Add, and then click Close.
Double-click the * character listed in the Customer table.
All the fields appear in the data pane.
On the File menu, click Return Data to Microsoft Excel.
In the Returning Data to Microsoft Excel dialog box, select New worksheet, and then click OK.
This creates the query table to use with the sample macro.
Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Partner or Microsoft Advisory Services. For more information, visit these Microsoft Web sites:
For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMSIn this example, you set up a Visual Basic for Applications macro thatresponds to the BeforeRefresh or AfterRefresh event in a worksheet. To set up the event handler, follow these steps:
Switch to the worksheet that contains the data from Microsoft Query.
Start the Visual Basic Editor (press ALT+F11).
If the Project Explorer window is not visible, click Project Explorer on the View menu.
On the Insert menu, click Class Module.
Click in the Code window for the class module and enter the following code:
Public WithEvents qt As QueryTablePrivate Sub qt_BeforeRefresh(Cancel As Boolean) ' Declare variables. Dim a As Integer Dim My_Prompt As String ' Initialize prompt text for message box. My_Prompt = "Data will be refreshed." ' Get YES or NO result from the message box a = MsgBox("Do you want to refresh the data now?", vbYesNo) ' Check to see whether YES or NO was selected. If a = vbNo Then ' Change prompt text for message box. My_Prompt = "Data will not be refreshed." ' Cancels the Query Refresh. Cancel = True End If ' Displays message box before refresh (or non-refresh) occurs. MsgBox My_PromptEnd Sub
NOTE: To use the AfterRefresh event, in the above example, you can replace the macro name "qt_BeforeRefresh(Cancel As Boolean)" with "qt_AfterRefresh(ByVal Success As Boolean)." Also, change the message box text to an appropriate post data-refresh message.
On the Insert menu, click Module.
Click in the Code window for the module and enter the following code:
Dim X As New Class1Sub Initialize_It() Set X.qt = Thisworkbook.Sheets(1).QueryTables(1)End Sub
Switch to Microsoft Excel (ALT+F11).
Click any cell in the worksheet query data.
On the Tools menu, point to Macro, and then click Macros. In the Macro dialog box, click Initialize_It and then click Run.
On the Data menu, click Refresh Data.
The BeforeRefresh event runs before the actual query data is refreshed and displays the message box that asks if you want to refresh the data. If you used the AfterRefresh event, the event runs after you refresh the query data.
NOTE: For the BeforeRefresh and AfterRefresh events to work, the Initialize_It macro must be run anytime the workbook is opened. You may want to use the name Auto_Open rather than Initialize_It. This allows the two events to function as expected when you refresh the query table data.