Use Excel VBA to Insert an AVERAGEIF Formula From Another Workbook
Question details
Insert an AVERAGEIF formula with VBA that references data in another workbook without copying it.

- Product
- Microsoft Excel VBA
- Device & OS
- not provided
- Scenario
- A macro needs to write a conditional average formula in a destination workbook using ranges in a source workbook.
- Observed behavior
- The desired formula references the source worksheet directly and places the result in the intended destination cell.
Identify the source workbook path, worksheet, criteria column, and values column, as well as the destination cell. Save the workbook containing the macro as an Excel macro-enabled workbook (.xlsm) and enable macros if prompted.
Write a fully qualified AVERAGEIF formula with VBA
Use this method to insert a formula that reads the source data directly without copying it.
The example checks column A for Approved and averages matching values in column D. It writes the formula to Results!B2 in the workbook containing the macro.
In Excel, open the destination macro-enabled workbook, then select Developer > Visual Basic and choose Insert > Module.
Paste the following code into the module: Sub InsertExternalAverageIf() Dim wbSource As Workbook Dim wsSource As Worksheet Dim lastRow As Long Dim sourceRef As String Set wbSource = Workbooks.Open("C:\Data\Source.xlsx") Set wsSource = wbSource.Worksheets("Source January") lastRow = wsSource.Cells(wsSource.Rows.Count, "A").End(xlUp).Row If lastRow < 2 Then MsgBox "No source data found below the header." Exit Sub End If sourceRef = "'" & Replace("[" & wbSource.Name & "]" & wsSource.Name, "'", "''") & "'!" ThisWorkbook.Worksheets("Results").Range("B2").Formula = _ "=AVERAGEIF(" & sourceRef & "$A$2:$A$" & lastRow & _ ",""Approved""," & sourceRef & "$D$2:$D$" & lastRow & ")" End Sub
Replace the source path, sheet names, columns, criterion, and destination cell with your own. Run InsertExternalAverageIf from Developer > Macros, then check Results!B2.

Work with conditional averages in WPS Spreadsheets
WPS Office is a free, lightweight alternative with a familiar interface and support for Excel formats, making it easier to move spreadsheet work between suites. You can use AVERAGEIF in WPS Spreadsheets, but WPS does not fix Excel VBA issues; VBA availability and compatibility depend on the WPS edition and environment.

Frequently Asked Questions
Why should I qualify Cells and Rows in the macro?
Qualifying them with wsSource, as in wsSource.Cells(wsSource.Rows.Count, "A"), ensures the last-row calculation uses the source worksheet rather than whichever sheet is active.
What does ThisWorkbook refer to?
ThisWorkbook is the workbook containing the VBA code. The example uses ThisWorkbook.Worksheets("Results").Range("B2") so the formula is written there, not to an active workbook.
Why does the formula return #DIV/0!?
AVERAGEIF returns #DIV/0! when no matching cells have numeric values to average. Check the criterion in column A and the corresponding values in column D.
What happens if the source workbook is moved?
Update the path in Workbooks.Open before running the macro. If an existing formula has a broken external link, check Excel's workbook links settings and update the source.




