VBA & Macro Problems

Use Excel VBA to Insert an AVERAGEIF Formula From Another Workbook

Steven ParkerSteven Parker Dec 2, 2026 868 views

Question details

Insert an AVERAGEIF formula with VBA that references data in another workbook without copying it.

Insert an AVERAGEIF Formula From Another Workbook With Excel VBA
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA editor

In Excel, open the destination macro-enabled workbook, then select Developer > Visual Basic and choose Insert > Module.

2
Add the macro

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

3
Adapt and run the macro

Replace the source path, sheet names, columns, criterion, and destination cell with your own. Run InsertExternalAverageIf from Developer > Macros, then check Results!B2.

Write a fully qualified AVERAGEIF formula with VBA
Check the source ranges: The last row is determined from column A, so that column must extend through the records you want to evaluate. AVERAGEIF can return #DIV/0! if no matching rows contain numeric values to average.
Free Microsoft Office alternative

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.

Use AVERAGEIF to calculate conditional averages in spreadsheets.Open and edit Microsoft Excel formats, including XLSX.Navigate a familiar spreadsheet interface.Use a free, lightweight alternative for everyday office tasks.
microsoft office alternative - wps office

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.